mirror of
https://github.com/ChronosX88/InstructorsListAppTA.git
synced 2024-12-04 22:52:19 +00:00
[backend] feat: Implement GetInstructorByID
This commit is contained in:
parent
38d05d3a89
commit
8ca8c3f73f
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using InstructorsListApp.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace InstructorsListApp.Controllers
|
||||
{
|
||||
@ -23,6 +24,16 @@ namespace InstructorsListApp.Controllers
|
||||
return databaseContext.Instructors.ToList();
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetInstructorByID(string id) {
|
||||
var instructor = databaseContext.Instructors.FirstOrDefault(i => i.Id == id);
|
||||
if(instructor != null) {
|
||||
databaseContext.Entry<Instructor>(instructor).State = EntityState.Detached; // remove unneccessary refs
|
||||
return Ok(instructor);
|
||||
}
|
||||
return NotFound("Given instructor isn't found");
|
||||
}
|
||||
|
||||
[HttpPost("add")]
|
||||
public IActionResult CreateInstructorEntry([FromBody] Instructor instructor) {
|
||||
if(ModelState.IsValid) {
|
||||
@ -40,7 +51,7 @@ namespace InstructorsListApp.Controllers
|
||||
instructor.Id = id;
|
||||
var instructorEntry = databaseContext.Instructors.FirstOrDefault(i => i.Id == id);
|
||||
if(instructorEntry != null) {
|
||||
databaseContext.Entry<Instructor>(instructorEntry).State = Microsoft.EntityFrameworkCore.EntityState.Detached;
|
||||
databaseContext.Entry<Instructor>(instructorEntry).State = EntityState.Detached; // remove unneccessary refs
|
||||
databaseContext.Instructors.Update(instructor);
|
||||
databaseContext.SaveChanges();
|
||||
return Ok();
|
||||
|
Loading…
Reference in New Issue
Block a user