Skip to content

Commit

Permalink
Merge pull request #14 from scogliera/Refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
scogliera authored Jan 16, 2024
2 parents e1f26d3 + af9ad80 commit e73a405
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 43 deletions.
30 changes: 18 additions & 12 deletions Controllers/TdaApiController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using TeacherDigitalAgency.DAL;
using TeacherDigitalAgency.Data;
using TeacherDigitalAgency.Models;

namespace TeacherDigitalAgency.Controllers;
Expand Down Expand Up @@ -91,17 +92,20 @@ public ActionResult<string> 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}")]
Expand All @@ -111,16 +115,18 @@ public ActionResult<string> 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)
{
_logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace);
return Problem(statusCode: 500);
}
}
}
}
9 changes: 5 additions & 4 deletions DAL/IMongoDal.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using TeacherDigitalAgency.Models;
using TeacherDigitalAgency.Data;
using TeacherDigitalAgency.Models;

namespace TeacherDigitalAgency.DAL;

public interface IMongoDal
{
public Lecturer? GetLecturer(Guid id);
public IEnumerable<Lecturer> 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);
}
90 changes: 63 additions & 27 deletions DAL/MongoDal.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using MongoDB.Driver;
using TeacherDigitalAgency.Data;
using TeacherDigitalAgency.Models;

namespace TeacherDigitalAgency.DAL;

public class MongoDal: IMongoDal
{
private readonly IMongoCollection<Lecturer> _lecturersCollection;
private readonly ILogger<MongoDal> _logger;

public MongoDal(IConfiguration configuration)
public MongoDal(IConfiguration configuration, ILogger<MongoDal> 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<Lecturer>("lecturers");
}
Expand All @@ -31,35 +35,67 @@ public IEnumerable<Lecturer> GetAllLecturers()
return _lecturersCollection.Find(_ => true).ToEnumerable();
}

public void SetLecturer(Lecturer lecturer, Guid uuid)
public DbResult SetLecturer(Lecturer lecturer, Guid uuid)
{
var filter = Builders<Lecturer>.Filter.Eq(l => l.Uuid, uuid);
var update = Builders<Lecturer>.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<Lecturer>.Filter.Eq(l => l.Uuid, uuid);
var update = Builders<Lecturer>.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;
}
}
}
}
8 changes: 8 additions & 0 deletions Data/DbResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TeacherDigitalAgency.Data;

public enum DbResult
{
Success,
NotFound,
Error
}

0 comments on commit e73a405

Please sign in to comment.