diff --git a/Controllers/TdaApiController.cs b/Controllers/TdaApiController.cs index 92b4a44..ef5408b 100644 --- a/Controllers/TdaApiController.cs +++ b/Controllers/TdaApiController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using TeacherDigitalAgency.DAL; +using TeacherDigitalAgency.Data; using TeacherDigitalAgency.Models; namespace TeacherDigitalAgency.Controllers; @@ -91,17 +92,20 @@ public ActionResult AddLecturerByUuid([FromBody] Lecturer lecturerNew, [ try { - _mongoDal.SetLecturer(lecturerNew, new Guid(uuid)); - return Ok("Lecturer nastaven, pokud existoval"); + var result = _mongoDal.SetLecturer(lecturerNew, new Guid(uuid)); + return result switch + { + DbResult.Success => Ok("Lecturer nastaven"), + DbResult.NotFound => NotFound("UUID nenalezeno"), + _ => Problem(statusCode: 500) + }; } 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"); + return Problem(statusCode: 500); } + } [HttpDelete("/lecturers/{uuid}")] @@ -111,11 +115,13 @@ public ActionResult DeleteLecturerByUuid([FromRoute] string uuid) try { - var success = _mongoDal.DeleteLecturer(new Guid(uuid)); - if (!success) - return NotFound("Lecturer nenalezen - UUID"); - - return NoContent(); + var result = _mongoDal.DeleteLecturer(new Guid(uuid)); + return result switch + { + DbResult.Success => NoContent(), + DbResult.NotFound => NotFound("UUID nenalezeno"), + _ => Problem(statusCode: 500) + }; } catch (Exception ex) { @@ -123,4 +129,4 @@ public ActionResult DeleteLecturerByUuid([FromRoute] string uuid) return Problem(statusCode: 500); } } -} \ No newline at end of file +} diff --git a/DAL/IMongoDal.cs b/DAL/IMongoDal.cs index c5bff8d..cd38399 100644 --- a/DAL/IMongoDal.cs +++ b/DAL/IMongoDal.cs @@ -1,4 +1,5 @@ -using TeacherDigitalAgency.Models; +using TeacherDigitalAgency.Data; +using TeacherDigitalAgency.Models; namespace TeacherDigitalAgency.DAL; @@ -6,7 +7,7 @@ public interface IMongoDal { public Lecturer? GetLecturer(Guid id); public IEnumerable GetAllLecturers(); - public void SetLecturer(Lecturer lecturer, Guid uuid); - public bool DeleteLecturer(Guid id); - public void AddLecturer(Lecturer lecturer); + public DbResult SetLecturer(Lecturer lecturer, Guid uuid); + public DbResult DeleteLecturer(Guid id); + public DbResult AddLecturer(Lecturer lecturer); } \ No newline at end of file diff --git a/DAL/MongoDal.cs b/DAL/MongoDal.cs index d6c04fc..b21da73 100644 --- a/DAL/MongoDal.cs +++ b/DAL/MongoDal.cs @@ -1,4 +1,5 @@ using MongoDB.Driver; +using TeacherDigitalAgency.Data; using TeacherDigitalAgency.Models; namespace TeacherDigitalAgency.DAL; @@ -6,17 +7,20 @@ namespace TeacherDigitalAgency.DAL; public class MongoDal: IMongoDal { private readonly IMongoCollection _lecturersCollection; + private readonly ILogger _logger; - public MongoDal(IConfiguration configuration) + public MongoDal(IConfiguration configuration, ILogger logger) { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + if (configuration == null) throw new ArgumentNullException(nameof(configuration)); - + var connectionString = configuration.GetConnectionString("MongoDb") ?? throw new ArgumentNullException(nameof(configuration)); - + var settings = MongoClientSettings.FromConnectionString(connectionString); settings.ServerApi = new ServerApi(ServerApiVersion.V1); - + var client = new MongoClient(settings); _lecturersCollection = client.GetDatabase("tda-db").GetCollection("lecturers"); } @@ -31,35 +35,67 @@ public IEnumerable GetAllLecturers() return _lecturersCollection.Find(_ => true).ToEnumerable(); } - public void SetLecturer(Lecturer lecturer, Guid uuid) + public DbResult SetLecturer(Lecturer lecturer, Guid uuid) { - var filter = Builders.Filter.Eq(l => l.Uuid, uuid); - var update = Builders.Update - .Set(l => l.TitleBefore, lecturer.TitleBefore) - .Set(l => l.FirstName, lecturer.FirstName) - .Set(l => l.MiddleName, lecturer.MiddleName) - .Set(l => l.LastName, lecturer.LastName) - .Set(l => l.TitleAfter, lecturer.TitleAfter) - .Set(l => l.PictureUrl, lecturer.PictureUrl) - .Set(l => l.Location, lecturer.Location) - .Set(l => l.Claim, lecturer.Claim) - .Set(l => l.Bio, lecturer.Bio) - .Set(l => l.Tags, lecturer.Tags) - .Set(l => l.PricePerHour, lecturer.PricePerHour) - .Set(l => l.ContactInfo, lecturer.ContactInfo); + try + { + var filter = Builders.Filter.Eq(l => l.Uuid, uuid); + var update = Builders.Update + .Set(l => l.TitleBefore, lecturer.TitleBefore) + .Set(l => l.FirstName, lecturer.FirstName) + .Set(l => l.MiddleName, lecturer.MiddleName) + .Set(l => l.LastName, lecturer.LastName) + .Set(l => l.TitleAfter, lecturer.TitleAfter) + .Set(l => l.PictureUrl, lecturer.PictureUrl) + .Set(l => l.Location, lecturer.Location) + .Set(l => l.Claim, lecturer.Claim) + .Set(l => l.Bio, lecturer.Bio) + .Set(l => l.Tags, lecturer.Tags) + .Set(l => l.PricePerHour, lecturer.PricePerHour) + .Set(l => l.ContactInfo, lecturer.ContactInfo); - _lecturersCollection.UpdateOne(filter, update); + var result = _lecturersCollection.UpdateOne(filter, update); + + if (result.MatchedCount <= 0) + return DbResult.NotFound; + + return DbResult.Success; + } + catch(Exception ex) + { + _logger.LogError("SetLecturer Error - Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return DbResult.Error; + } } - public bool DeleteLecturer(Guid id) + public DbResult 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; + try + { + var result = _lecturersCollection.DeleteOne(lecturer => lecturer.Uuid == id); + if (result.DeletedCount <= 0) + return DbResult.NotFound; + + return DbResult.Success; + } + catch(Exception ex) + { + _logger.LogError("DeleteLecturer Error - Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return DbResult.Error; + } } - public void AddLecturer(Lecturer lecturer) + public DbResult AddLecturer(Lecturer lecturer) { - _lecturersCollection.InsertOne(lecturer); + try + { + _lecturersCollection.InsertOne(lecturer); + return DbResult.Success; + } + catch(Exception ex) + { + _logger.LogError("AddLecturer Error - Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace); + return DbResult.Error; + } } -} \ No newline at end of file +} diff --git a/Data/DbResult.cs b/Data/DbResult.cs new file mode 100644 index 0000000..b715cc3 --- /dev/null +++ b/Data/DbResult.cs @@ -0,0 +1,8 @@ +namespace TeacherDigitalAgency.Data; + +public enum DbResult +{ + Success, + NotFound, + Error +}