From 8ca8c3f73fd6a24d3065eb83ae727fc051ca49f7 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 25 Sep 2019 19:36:32 +0400 Subject: [PATCH 1/2] [backend] feat: Implement GetInstructorByID --- Controllers/InstructorsDataController.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Controllers/InstructorsDataController.cs b/Controllers/InstructorsDataController.cs index f1702d8..3f5db8a 100644 --- a/Controllers/InstructorsDataController.cs +++ b/Controllers/InstructorsDataController.cs @@ -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).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(instructorEntry).State = Microsoft.EntityFrameworkCore.EntityState.Detached; + databaseContext.Entry(instructorEntry).State = EntityState.Detached; // remove unneccessary refs databaseContext.Instructors.Update(instructor); databaseContext.SaveChanges(); return Ok(); From 6e1d88e2e76ccf455b3b75a2a9c0615dbb74cb39 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 25 Sep 2019 19:50:01 +0400 Subject: [PATCH 2/2] [backend] feat: Add error model for error responses, add checking on invalid post body (if all fields of Instructor is null) --- Controllers/InstructorsDataController.cs | 11 +++++++---- Models/Error.cs | 14 ++++++++++++++ Models/Instructor.cs | 10 +++++----- 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 Models/Error.cs diff --git a/Controllers/InstructorsDataController.cs b/Controllers/InstructorsDataController.cs index 3f5db8a..6ec9a6a 100644 --- a/Controllers/InstructorsDataController.cs +++ b/Controllers/InstructorsDataController.cs @@ -31,18 +31,21 @@ namespace InstructorsListApp.Controllers databaseContext.Entry(instructor).State = EntityState.Detached; // remove unneccessary refs return Ok(instructor); } - return NotFound("Given instructor isn't found"); + return NotFound(new Error(Error.EntryIsNotFound, "No such instructor")); } [HttpPost("add")] public IActionResult CreateInstructorEntry([FromBody] Instructor instructor) { if(ModelState.IsValid) { + if(instructor.FirstName == null && instructor.LastName == null && instructor.MiddleName == null) { + return BadRequest(new Error(Error.PostBodyIsNotValid, "Given model isn't valid!")); + } instructor.Id = Guid.NewGuid().ToString(); databaseContext.Instructors.Add(instructor); databaseContext.SaveChanges(); return Ok(instructor.Id); } - return BadRequest(ModelState); + return BadRequest(new Error(Error.PostBodyIsNotValid, "Given model isn't valid!")); } [HttpPut("{id}")] @@ -56,7 +59,7 @@ namespace InstructorsListApp.Controllers databaseContext.SaveChanges(); return Ok(); } - return NotFound("Given instructor isn't found"); + return NotFound(new Error(Error.EntryIsNotFound, "No such instructor")); } return BadRequest(ModelState); } @@ -69,7 +72,7 @@ namespace InstructorsListApp.Controllers databaseContext.SaveChanges(); return Ok(); } - return NotFound("No such instructor"); + return NotFound(new Error(Error.EntryIsNotFound, "No such instructor")); } } } diff --git a/Models/Error.cs b/Models/Error.cs new file mode 100644 index 0000000..05b4e8e --- /dev/null +++ b/Models/Error.cs @@ -0,0 +1,14 @@ +namespace InstructorsListApp.Models { + public class Error { + public static int EntryIsNotFound = 0x0; + public static int PostBodyIsNotValid = 0x1; + + public int errCode {get; set;} + public string errText {get; set;} + + public Error(int errCode, string errText) { + this.errCode = errCode; + this.errText = errText; + } + } +} \ No newline at end of file diff --git a/Models/Instructor.cs b/Models/Instructor.cs index d944063..7a4bc8d 100644 --- a/Models/Instructor.cs +++ b/Models/Instructor.cs @@ -1,8 +1,8 @@ namespace InstructorsListApp.Models { public class Instructor { - public string Id {get; set;} - public string FirstName {get;set;} - public string MiddleName {get;set;} - public string LastName {get;set;} -} + public string Id {get; set;} + public string FirstName {get;set;} + public string MiddleName {get;set;} + public string LastName {get;set;} + } } \ No newline at end of file