diff --git a/Controllers/TdaApiController.cs b/Controllers/TdaApiController.cs index 9bf1987..92b4a44 100644 --- a/Controllers/TdaApiController.cs +++ b/Controllers/TdaApiController.cs @@ -18,9 +18,9 @@ public TdaApiController(ILogger logger, IMongoDal mongoDal) } [HttpGet("/api")] - public ActionResult> OnGet() + public ActionResult> OnBaseApiCall() { - _logger.LogDebug("Base api endpoint called"); + _logger.LogDebug("[GET] Base /api called"); var result = new Dictionary { @@ -30,35 +30,97 @@ public ActionResult> OnGet() return Ok(result); } - [HttpPost("/api")] - public ActionResult> OnPost() + [HttpPost("/lecturers")] + public ActionResult AddLecturer([FromBody] Lecturer lecturer) { - _logger.LogDebug("Base api post endpoint called"); + _logger.LogDebug("[POST] AddLecturer called"); - var result = new Dictionary + try { - { "secret", "The cake is a lie" } - }; + _mongoDal.AddLecturer(lecturer); + return Ok("Lecturer přidán"); + } + catch (Exception ex) + { + _logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return Problem(statusCode: 500); + } + } - return Ok(result); + [HttpGet("/lecturers")] + public ActionResult> GetAllLecturers() + { + _logger.LogDebug("[GET] GetAllLecturers called"); + + try + { + var lecturers = _mongoDal.GetAllLecturers(); + return Ok(lecturers); + } + catch (Exception ex) + { + _logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return Problem(statusCode: 500); + } } - [HttpPost("/prikladvseho/{vek:int}")] - public ActionResult> PrikladVseho([FromRoute] int vek, [FromBody] Lecturer lecturer) + [HttpGet("/lecturers/{uuid}")] + public ActionResult GetLecturerByUuid([FromRoute] string uuid) { - var anotherLecturer = new Lecturer + _logger.LogDebug("[GET] GetLecturerByUuid called"); + + try { - FirstName = "Jozef", - LastName = "Novák", - TitleBefore = "Ing." - }; - - var informationList = new List(); - - informationList.Add($"{lecturer.FirstName} má {vek} let."); - informationList.Add($"Jeho ID je {lecturer.Uuid}"); - informationList.Add($"{lecturer.FirstName} má kamaráda {anotherLecturer.FirstName} {anotherLecturer.LastName}"); - - return Ok(informationList); + var lecturer = _mongoDal.GetLecturer(new Guid(uuid)); + if (lecturer == null) + return NotFound("Lecturer nenalezen - UUID"); + + return Ok(lecturer); + } + catch (Exception ex) + { + _logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return Problem(statusCode: 500); + } + } + + [HttpPut("/lecturers/{uuid}")] + public ActionResult AddLecturerByUuid([FromBody] Lecturer lecturerNew, [FromRoute] string uuid) + { + _logger.LogDebug("[PUT] AddLecturerByUuid called"); + + try + { + _mongoDal.SetLecturer(lecturerNew, new Guid(uuid)); + return Ok("Lecturer nastaven, pokud existoval"); + } + catch(Exception ex) + { + _logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + + //TODO: Odlišovat error u nás od nenalezena (Problem x NotFound) + //return Problem(statusCode: 500); + return NotFound("UUID nenalezeno"); + } + } + + [HttpDelete("/lecturers/{uuid}")] + public ActionResult DeleteLecturerByUuid([FromRoute] string uuid) + { + _logger.LogDebug("[DELETE] DeleteLecturerByUuid called"); + + try + { + var success = _mongoDal.DeleteLecturer(new Guid(uuid)); + if (!success) + return NotFound("Lecturer nenalezen - UUID"); + + return NoContent(); + } + catch (Exception ex) + { + _logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return Problem(statusCode: 500); + } } } \ No newline at end of file diff --git a/DAL/MongoDal.cs b/DAL/MongoDal.cs index 92f61cb..d6c04fc 100644 --- a/DAL/MongoDal.cs +++ b/DAL/MongoDal.cs @@ -53,6 +53,7 @@ public void SetLecturer(Lecturer lecturer, Guid uuid) public bool DeleteLecturer(Guid id) { + // TODO: Refactor return value - distinguish NotFound, Error and Success var deleteResult = _lecturersCollection.DeleteOne(lecturer => lecturer.Uuid == id); return deleteResult.DeletedCount > 0 && deleteResult.IsAcknowledged; }