From e5d67f28c6ac7f47653364f629eb7509f119abef Mon Sep 17 00:00:00 2001 From: mkoper02 Date: Mon, 29 Apr 2024 20:08:29 +0200 Subject: [PATCH] Final fixes and changed unit tests for methods with foreign keys --- .../Services/CommentServiceTests.cs | 31 ++++++--------- .../Services/ParticipantServiceTests.cs | 38 +++++++++---------- .../Services/UserServiceTests.cs | 26 ++++++------- .../ReasnAPI/Services/CommentService.cs | 10 ++--- .../ReasnAPI/Services/ObjectTypeService.cs | 2 +- .../ReasnAPI/Services/ParticipantService.cs | 14 +++---- .../ReasnAPI/ReasnAPI/Services/UserService.cs | 27 ++++++------- 7 files changed, 70 insertions(+), 78 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs index f519e9b3..d17dded6 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs @@ -27,9 +27,8 @@ public void GetCommentById_CommentExist_CommentReturned() { var comment = new Comment() { Id = 1, Content = "Content", - CreatedAt = DateTime.Now, - Event = event1, - User = user + EventId = event1.Id, + UserId = user.Id }; mockContext.Setup(c => c.Users).ReturnsDbSet([ user ]); @@ -78,17 +77,15 @@ public void GetAllComments_CommentsExist_CommentsReturned() { var comment1 = new Comment() { Id = 1, Content = "Content", - CreatedAt = DateTime.Now, - Event = event1, - User = user + EventId = event1.Id, + UserId = user.Id }; var comment2 = new Comment() { Id = 2, Content = "Content", - CreatedAt = DateTime.Now, - Event = event1, - User = user + EventId = event1.Id, + UserId = user.Id }; mockContext.Setup(c => c.Users).ReturnsDbSet([ user ]); @@ -136,19 +133,15 @@ public void GetCommentsByFilter_CommentsExist_CommentsReturned() { var comment1 = new Comment() { Id = 1, Content = "Content", - CreatedAt = DateTime.Now, - Event = event1, EventId = event1.Id, - User = user + UserId = user.Id }; var comment2 = new Comment() { Id = 2, Content = "Content", - CreatedAt = DateTime.Now, - Event = event1, EventId = event1.Id, - User = user + UserId = user.Id }; mockContext.Setup(c => c.Users).ReturnsDbSet([user]); @@ -157,7 +150,7 @@ public void GetCommentsByFilter_CommentsExist_CommentsReturned() { var commentService = new CommentService(mockContext.Object); - var result = commentService.GetCommentsByFilter(r => r.Event.Id == 1); + var result = commentService.GetCommentsByFilter(r => r.EventId == 1); Assert.IsNotNull(result); Assert.AreEqual(2, result.Count()); @@ -245,9 +238,8 @@ public void UpdateComment_CommentUpdated_CommentReturned() { var comment = new Comment() { Id = 1, Content = "Content", - CreatedAt = DateTime.Now, - Event = event1, - User = user + EventId = event1.Id, + UserId = user.Id }; mockContext.Setup(c => c.Users).ReturnsDbSet([ user ]); @@ -303,7 +295,6 @@ public void DeleteComment_CommentExists_CommentDeleted() { var comment = new Comment() { Id = 1, Content = "Content", - CreatedAt = DateTime.Now, EventId = 1, UserId = 1 }; diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParticipantServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParticipantServiceTests.cs index 1918046d..4481758f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParticipantServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParticipantServiceTests.cs @@ -31,9 +31,9 @@ public void GetParticipantById_ParticipantExists_ParticipantReturned() { var participant = new Participant { Id = 1, - Event = event1, - User = user, - Status = status + EventId = event1.Id, + UserId = user.Id, + StatusId = status.Id }; mockContext.Setup(c => c.Events).ReturnsDbSet([ event1 ]); @@ -94,16 +94,16 @@ public void GetAllParticipants_ParticipantsExist_ParticipantsReturned() { var participant1 = new Participant { Id = 1, - Event = event1, - User = user1, - Status = status + EventId = event1.Id, + UserId = user1.Id, + StatusId = status.Id }; var participant2 = new Participant { Id = 2, - Event = event1, - User = user2, - Status = status + EventId = event1.Id, + UserId = user2.Id, + StatusId = status.Id }; mockContext.Setup(c => c.Events).ReturnsDbSet([ event1 ]); @@ -163,16 +163,16 @@ public void GetParticipantsByFilter_ParticipantsExist_ParticipantsReturned() { var participant1 = new Participant { Id = 1, - Event = event1, - User = user1, - Status = status + EventId = event1.Id, + UserId = user1.Id, + StatusId = status.Id }; var participant2 = new Participant { Id = 2, - Event = event1, - User = user2, - Status = status + EventId = event1.Id, + UserId = user2.Id, + StatusId = status.Id }; mockContext.Setup(c => c.Events).ReturnsDbSet([event1]); @@ -182,7 +182,7 @@ public void GetParticipantsByFilter_ParticipantsExist_ParticipantsReturned() { var participantService = new ParticipantService(mockContext.Object); - var result = participantService.GetParticipantsByFilter(r => r.Event.Id == 1); + var result = participantService.GetParticipantsByFilter(r => r.EventId == 1); Assert.IsNotNull(result); Assert.AreEqual(2, result.Count()); @@ -285,9 +285,9 @@ public void UpdateParticipant_ParticipantExists_ParticipantReturned() { var participant = new Participant { Id = 1, - Event = event1, - User = user, - Status = status1 + EventId = event1.Id, + UserId = user.Id, + StatusId = status1.Id }; mockContext.Setup(c => c.Events).ReturnsDbSet([ event1 ]); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/UserServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/UserServiceTests.cs index 670ae0e8..600ce844 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/UserServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/UserServiceTests.cs @@ -26,13 +26,13 @@ public void GetUserById_UserExist_UserReturned() { }; var user = new User() { - Id = 1, + Id = 1, Name = "John", Surname = "Doe", Username = "Username", Email = "Email", - Address = address, - Role = role + AddressId = address.Id, + RoleId = address.Id }; mockContext.Setup(c => c.Addresses).ReturnsDbSet([ address ]); @@ -78,7 +78,7 @@ public void GetAllUsers_UsersExist_UsersReturned() { Surname = "Doe", Username = "Username", Email = "Email", - Role = role + RoleId = role.Id }; var user2 = new User() { @@ -87,7 +87,7 @@ public void GetAllUsers_UsersExist_UsersReturned() { Surname = "Doe", Username = "Username", Email = "Email", - Role = role + RoleId = role.Id }; mockContext.Setup(c => c.Users).ReturnsDbSet([ user1, user2 ]); @@ -128,7 +128,7 @@ public void GetUsersByFilter_UsersExist_UsersReturned() { Surname = "Doe", Username = "Username", Email = "Email", - Role = role + RoleId = role.Id }; var user2 = new User() { @@ -137,7 +137,7 @@ public void GetUsersByFilter_UsersExist_UsersReturned() { Surname = "Doe", Username = "Username", Email = "Email", - Role = role + RoleId = role.Id }; mockContext.Setup(c => c.Users).ReturnsDbSet([ user1, user2]); @@ -193,8 +193,8 @@ public void CreateUser_UserCreated_UserReturned() { Username = "Username", Email = "Email", Phone = "Phone", - RoleId = 1, - AddressId = 1 + RoleId = role.Id, + AddressId = address.Id }; var result = userService.CreateUser(userDto); @@ -276,8 +276,8 @@ public void UpdateUser_UserUpdated_UserReturned() { Surname = "Doe", Username = "Username", Email = "Email", - Address = address, - Role = role + AddressId = address.Id, + RoleId = role.Id }; mockContext.Setup(c => c.Addresses).ReturnsDbSet([ address ]); @@ -292,8 +292,8 @@ public void UpdateUser_UserUpdated_UserReturned() { Username = "Username", Email = "Email", Phone = "Phone", - RoleId = 1, - AddressId = 1 + RoleId = role.Id, + AddressId = address.Id }; var result = userService.UpdateUser(1, userDto); diff --git a/Server/ReasnAPI/ReasnAPI/Services/CommentService.cs b/Server/ReasnAPI/ReasnAPI/Services/CommentService.cs index a010e78a..adc98e2a 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/CommentService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/CommentService.cs @@ -15,7 +15,7 @@ public class CommentService (ReasnContext context) { Content = commentDto.Content, UserId = commentDto.UserId, - CreatedAt = DateTime.Now + CreatedAt = DateTime.UtcNow }; _context.Comments.Add(comment); @@ -68,11 +68,11 @@ public void DeleteComment(int commentId) { private static CommentDto? MapToCommentDto(Comment? comment) { if (comment is null) - return null; - + return null; + return new CommentDto { - EventId = comment.Event.Id, - UserId = comment.User.Id, + EventId = comment.EventId, + UserId = comment.UserId, Content = comment.Content, CreatedAt = comment.CreatedAt }; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ObjectTypeService.cs b/Server/ReasnAPI/ReasnAPI/Services/ObjectTypeService.cs index 7d8841fb..c939c412 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ObjectTypeService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ObjectTypeService.cs @@ -30,7 +30,7 @@ public class ObjectTypeService(ReasnContext context) { if (objectTypeDb is null) return null; - _context.ObjectTypes.Update(objectType); + objectTypeDb.Name = objectType.Name; _context.SaveChanges(); return objectType; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParticipantService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParticipantService.cs index 1a5804a3..0a21d777 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParticipantService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParticipantService.cs @@ -24,9 +24,9 @@ public class ParticipantService (ReasnContext context) { return null; var participant = new Participant() { - Event = eventDb, - User = userDb, - Status = statusDb + EventId = participantDto.EventId, + UserId = participantDto.UserId, + StatusId = participantDto.StatusId }; _context.Participants.Add(participant); @@ -47,7 +47,7 @@ public class ParticipantService (ReasnContext context) { if (status is null) return null; - participant.Status = status; + participant.StatusId = participantDto.StatusId; _context.SaveChanges(); @@ -85,9 +85,9 @@ public void DeleteParticipant(int participantId) { return null; return new ParticipantDto { - EventId = participant.Event.Id, - UserId = participant.User.Id, - StatusId = participant.Status.Id + EventId = participant.EventId, + UserId = participant.UserId, + StatusId = participant.StatusId }; } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/UserService.cs b/Server/ReasnAPI/ReasnAPI/Services/UserService.cs index a1e70386..6702d235 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/UserService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/UserService.cs @@ -6,7 +6,9 @@ namespace ReasnAPI.Services { public class UserService (ReasnContext context) { private readonly ReasnContext _context = context; - // TODO: create, update, delete user's interests + // TODO: + // * create, update, delete user's interests + // * fix delete service public UserDto? CreateUser(UserDto? userDto) { if (userDto is null) @@ -27,7 +29,7 @@ public class UserService (ReasnContext context) { RoleId = userDto.RoleId, AddressId = userDto.AddressId, - CreatedAt = DateTime.Now, + CreatedAt = DateTime.UtcNow, IsActive = true }; @@ -54,7 +56,7 @@ public class UserService (ReasnContext context) { user.RoleId = userDto.RoleId; user.AddressId = userDto.AddressId; - user.UpdatedAt = DateTime.Now; + user.UpdatedAt = DateTime.UtcNow; _context.SaveChanges(); @@ -91,16 +93,15 @@ public void DeleteUser(int userId) { if (user is null) return null; - var userDto = new UserDto(); - - userDto.Username = user.Username; - userDto.Name = user.Name; - userDto.Surname = user.Surname; - userDto.Email = user.Email; - userDto.Phone = user.Phone; - userDto.RoleId = user.Role.Id; - if (user.Address is not null) - userDto.AddressId = user.Address.Id; + var userDto = new UserDto { + Username = user.Username, + Name = user.Name, + Surname = user.Surname, + Email = user.Email, + Phone = user.Phone, + RoleId = user.RoleId, + AddressId = user.AddressId + }; return userDto; }