mirror of
https://github.com/ChronosX88/InstructorsListAppTA.git
synced 2024-12-04 14:42:18 +00:00
[backend] feat: Add error model for error responses, add checking on invalid post body (if all fields of Instructor is null)
This commit is contained in:
parent
8ca8c3f73f
commit
6e1d88e2e7
@ -31,18 +31,21 @@ namespace InstructorsListApp.Controllers
|
||||
databaseContext.Entry<Instructor>(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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
Models/Error.cs
Normal file
14
Models/Error.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user