From 07596a597cec996304d1a288a4a5373c5ab86897 Mon Sep 17 00:00:00 2001 From: 1billy17 Date: Thu, 14 Nov 2024 16:33:46 +0300 Subject: [PATCH] logs --- data/Repository/PresenceRepositoryImpl.cs | 21 +++++++++++++++++-- presence_api/Controllers/AdminController.cs | 13 +++++++++++- .../Controllers/PresenceController.cs | 14 ++++++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/data/Repository/PresenceRepositoryImpl.cs b/data/Repository/PresenceRepositoryImpl.cs index e9b0f5f..51030a7 100644 --- a/data/Repository/PresenceRepositoryImpl.cs +++ b/data/Repository/PresenceRepositoryImpl.cs @@ -40,6 +40,11 @@ namespace Demo.Data.Repository var allRecords = _remoteDatabaseContext.PresenceDaos.ToList(); _remoteDatabaseContext.PresenceDaos.RemoveRange(allRecords); _remoteDatabaseContext.SaveChanges(); + + if (allRecords.Count == 0) { + return false; + } + return true; } @@ -51,26 +56,38 @@ namespace Demo.Data.Repository _remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords); _remoteDatabaseContext.SaveChanges(); + if (delRecords.Count == 0) { + return false; + } + return true; } public bool DeletePresenceByGroup(int groupID) { var delRecords = _remoteDatabaseContext.PresenceDaos - .Where(y => y.userDAO.GroupID == groupID); + .Where(y => y.userDAO.GroupID == groupID).ToList(); _remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords); _remoteDatabaseContext.SaveChanges(); + if (delRecords.Count == 0) { + return false; + } + return true; } public bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate) { var delRecords = _remoteDatabaseContext.PresenceDaos - .Where(x => x.Date >= startDate && x.Date <= endDate); + .Where(x => x.Date >= startDate && x.Date <= endDate).ToList(); _remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords); _remoteDatabaseContext.SaveChanges(); + if (delRecords.Count == 0) { + return false; + } + return true; } diff --git a/presence_api/Controllers/AdminController.cs b/presence_api/Controllers/AdminController.cs index c3a6061..cf442aa 100644 --- a/presence_api/Controllers/AdminController.cs +++ b/presence_api/Controllers/AdminController.cs @@ -11,10 +11,12 @@ public class AdminController: ControllerBase{ private readonly GroupUseCase _groupUseCase; private readonly UserUseCase _userUseCase; private readonly PresenceUseCase _presenceUseCase; - public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase){ + private readonly ILogger _logger; + public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase, ILogger logger){ _groupUseCase = groupUseCase; _userUseCase = userUseCase; _presenceUseCase = presenceUseCase; + _logger = logger; } //post @@ -23,6 +25,7 @@ public class AdminController: ControllerBase{ { if (request == null || string.IsNullOrEmpty(request.GroupName)) { + _logger.LogWarning("Invalid request"); return BadRequest("Invalid request"); } @@ -40,10 +43,12 @@ public class AdminController: ControllerBase{ [HttpDelete("user")] public ActionResult DeleteUser(Guid userGuid){ if (userGuid == Guid.Empty){ + _logger.LogWarning("Invalid request"); return BadRequest("Invalid request"); } bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid); if (isDeleted == false){ + _logger.LogWarning("User not found"); return NotFound("User not found"); } return Ok(true); @@ -52,12 +57,14 @@ public class AdminController: ControllerBase{ [HttpDelete("users")] public ActionResult DeleteUsers([FromBody] DeleteUsersRequest request){ if (request == null){ + _logger.LogWarning("Invalid request"); return BadRequest("Invalid request"); } foreach (Guid userGuid in request.UsersGuid) { bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid); if (isDeleted == false){ + _logger.LogWarning("User not found"); return NotFound("User not found"); } } @@ -68,6 +75,7 @@ public class AdminController: ControllerBase{ public ActionResult DeleteGroup(int GroupID){ bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID); if (isDeleted == false){ + _logger.LogWarning("Group not found"); return NotFound("Group not found"); } return Ok(true); @@ -76,12 +84,14 @@ public class AdminController: ControllerBase{ [HttpDelete("groups")] public ActionResult DeleteGroups([FromBody] DeleteGroupsRequest request){ if (request == null){ + _logger.LogWarning("Invalid request"); return BadRequest("Invalid request"); } foreach (int GroupID in request.GroupIDs) { bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID); if (isDeleted == false){ + _logger.LogWarning("Group not found"); return NotFound("Group not found"); } } @@ -107,6 +117,7 @@ public class AdminController: ControllerBase{ if (user != null){ return Ok(user); } else{ + _logger.LogWarning("User not found"); return NotFound("User not found"); } } diff --git a/presence_api/Controllers/PresenceController.cs b/presence_api/Controllers/PresenceController.cs index 56ae8b2..4cba947 100644 --- a/presence_api/Controllers/PresenceController.cs +++ b/presence_api/Controllers/PresenceController.cs @@ -1,7 +1,6 @@ using Demo.Domain.Models; using Demo.Domain.UseCase; using Microsoft.AspNetCore.Mvc; -using Npgsql.TypeMapping; namespace presence_api.Controllers; [ApiController] @@ -10,10 +9,12 @@ namespace presence_api.Controllers; public class PresenceController: ControllerBase{ private readonly PresenceUseCase _presenceUseCase; + private readonly ILogger _logger; - public PresenceController(PresenceUseCase presenceUseCase){ + public PresenceController(PresenceUseCase presenceUseCase, ILogger logger){ _presenceUseCase = presenceUseCase; + _logger = logger; } @@ -22,6 +23,7 @@ public class PresenceController: ControllerBase{ if (!DateOnly.TryParse(date, out var parseDate)) { + _logger.LogWarning("Invalid date format"); return BadRequest("Invalid date format"); } @@ -48,11 +50,13 @@ public class PresenceController: ControllerBase{ public ActionResult DeletePresenceByUserGuid(Guid userGuid) { if (userGuid == Guid.Empty){ + _logger.LogWarning("Invalid Guid"); return BadRequest("Invalid request"); } var isDeleted = _presenceUseCase.DeletePresenceByUser(userGuid); if (isDeleted == false){ + _logger.LogWarning("User not found"); return NotFound("User not found"); } return Ok(true); @@ -63,6 +67,7 @@ public class PresenceController: ControllerBase{ { var isDeleted = _presenceUseCase.DeletePresenceByGroup(groupID); if (isDeleted == false){ + _logger.LogWarning("Group not found"); return NotFound("Group not found"); } return Ok(true); @@ -74,11 +79,13 @@ public class PresenceController: ControllerBase{ if (!DateOnly.TryParse(startDate, out var parsedStartDate) || !DateOnly.TryParse(endDate, out var parsedEndDate)) { + _logger.LogWarning("Invalid date format"); return BadRequest("Invalid date format"); } var isDeleted = _presenceUseCase.DeletePresenceByDateRange(parsedStartDate, parsedEndDate); if (isDeleted == false){ + _logger.LogWarning("Presence not found"); return NotFound("Presence not found"); } @@ -90,15 +97,16 @@ public class PresenceController: ControllerBase{ { if (!DateOnly.TryParse(date, out var parseDate)) { + _logger.LogWarning("Invalid date format"); return BadRequest("Invalid date format"); } var isUpdated = _presenceUseCase.IsAttedance(firstLesson, lastLesson, parseDate, UserGuid); if (isUpdated == false){ + _logger.LogWarning("Presence not found"); return NotFound("Presence not found"); } return Ok(true); } - } \ No newline at end of file