Skip to content

Commit

Permalink
Final fixes and changed unit tests for methods with foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoper02 committed Apr 29, 2024
1 parent b6f34d6 commit e5d67f2
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 78 deletions.
31 changes: 11 additions & 20 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]);
Expand Down Expand Up @@ -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 ]);
Expand Down Expand Up @@ -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]);
Expand All @@ -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());
Expand Down Expand Up @@ -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 ]);
Expand Down Expand Up @@ -303,7 +295,6 @@ public void DeleteComment_CommentExists_CommentDeleted() {
var comment = new Comment() {
Id = 1,
Content = "Content",
CreatedAt = DateTime.Now,
EventId = 1,
UserId = 1
};
Expand Down
38 changes: 19 additions & 19 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/ParticipantServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]);
Expand Down Expand Up @@ -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 ]);
Expand Down Expand Up @@ -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]);
Expand All @@ -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());
Expand Down Expand Up @@ -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 ]);
Expand Down
26 changes: 13 additions & 13 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/UserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]);
Expand Down Expand Up @@ -78,7 +78,7 @@ public void GetAllUsers_UsersExist_UsersReturned() {
Surname = "Doe",
Username = "Username",
Email = "Email",
Role = role
RoleId = role.Id
};

var user2 = new User() {
Expand All @@ -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 ]);
Expand Down Expand Up @@ -128,7 +128,7 @@ public void GetUsersByFilter_UsersExist_UsersReturned() {
Surname = "Doe",
Username = "Username",
Email = "Email",
Role = role
RoleId = role.Id
};

var user2 = new User() {
Expand All @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 ]);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions Server/ReasnAPI/ReasnAPI/Services/CommentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion Server/ReasnAPI/ReasnAPI/Services/ObjectTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions Server/ReasnAPI/ReasnAPI/Services/ParticipantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -47,7 +47,7 @@ public class ParticipantService (ReasnContext context) {
if (status is null)
return null;

participant.Status = status;
participant.StatusId = participantDto.StatusId;

_context.SaveChanges();

Expand Down Expand Up @@ -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
};
}
}
Expand Down
27 changes: 14 additions & 13 deletions Server/ReasnAPI/ReasnAPI/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,7 +29,7 @@ public class UserService (ReasnContext context) {
RoleId = userDto.RoleId,
AddressId = userDto.AddressId,

CreatedAt = DateTime.Now,
CreatedAt = DateTime.UtcNow,
IsActive = true
};

Expand All @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e5d67f2

Please sign in to comment.