Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added brackets one line ifs and added delete method for ObjectTypeSer…
Browse files Browse the repository at this point in the history
…vice
mkoper02 committed May 6, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 034e2cb commit 1a58a27
Showing 6 changed files with 127 additions and 56 deletions.
30 changes: 20 additions & 10 deletions Server/ReasnAPI/ReasnAPI/Services/AddressService.cs
Original file line number Diff line number Diff line change
@@ -10,8 +10,10 @@ public class AddressService(ReasnContext context)

public AddressDto? CreateAddress(AddressDto? addressDto)
{
if (addressDto is null)
return null;
if (addressDto is null)
{
return null;
}

var address = new Address
{
@@ -30,13 +32,17 @@ public class AddressService(ReasnContext context)

public AddressDto? UpdateAddress(int addressId, AddressDto? addressDto)
{
if (addressDto is null)
return null;
if (addressDto is null)
{
return null;
}

var address = _context.Addresses.FirstOrDefault(r => r.Id == addressId);

if (address is null)
return null;
if (address is null)
{
return null;
}

address.City = addressDto.City;
address.Country = addressDto.Country;
@@ -53,8 +59,10 @@ public bool DeleteAddress(int addressId)
{
var address = _context.Addresses.FirstOrDefault(r => r.Id == addressId);

if (address is null)
return false ;
if (address is null)
{
return false ;
}

_context.Addresses.Remove(address);
_context.SaveChanges();
@@ -66,8 +74,10 @@ public bool DeleteAddress(int addressId)
{
var address = _context.Addresses.FirstOrDefault(r => r.Id == addressId);

if (address is null)
return null;
if (address is null)
{
return null;
}

return MapToAddressDto(address);
}
20 changes: 15 additions & 5 deletions Server/ReasnAPI/ReasnAPI/Services/CommentService.cs
Original file line number Diff line number Diff line change
@@ -10,8 +10,10 @@ public class CommentService(ReasnContext context)

public CommentDto? CreateComment(CommentDto? commentDto)
{
if (commentDto is null)
if (commentDto is null)
{
return null;
}

var comment = new Comment
{
@@ -29,13 +31,17 @@ public class CommentService(ReasnContext context)

public CommentDto? UpdateComment(int commentId, CommentDto? commentDto)
{
if (commentDto is null)
if (commentDto is null)
{
return null;
}

var comment = _context.Comments.FirstOrDefault(r => r.Id == commentId);

if (comment is null)
if (comment is null)
{
return null;
}

comment.Content = commentDto.Content;

@@ -48,8 +54,10 @@ public bool DeleteComment(int commentId)
{
var comment = _context.Comments.FirstOrDefault(r => r.Id == commentId);

if (comment is null)
if (comment is null)
{
return false;
}

_context.Comments.Remove(comment);
_context.SaveChanges();
@@ -61,8 +69,10 @@ public bool DeleteComment(int commentId)
{
var comment = _context.Comments.FirstOrDefault(r => r.Id == commentId);

if (comment is null)
if (comment is null)
{
return null;
}

return MapToCommentDto(comment);
}
31 changes: 21 additions & 10 deletions Server/ReasnAPI/ReasnAPI/Services/ObjectTypeService.cs
Original file line number Diff line number Diff line change
@@ -9,14 +9,18 @@ public class ObjectTypeService(ReasnContext context)

public ObjectType? CreateObjectType(ObjectType? objectType)
{
if (objectType is null)
return null;
if (objectType is null)
{
return null;
}

// check if object type with the same name already exists
var objectTypeDb = _context.ObjectTypes.FirstOrDefault(r => r.Name == objectType.Name);

if (objectTypeDb is not null)
if (objectTypeDb is not null)
{
return null;
}

_context.ObjectTypes.Add(objectType);
_context.SaveChanges();
@@ -26,30 +30,37 @@ public class ObjectTypeService(ReasnContext context)

public ObjectType? UpdateObjectType(ObjectType? objectType)
{
if (objectType is null)
return null;
if (objectType is null)
{
return null;
}

var objectTypeDb = _context.ObjectTypes.FirstOrDefault(r => r.Id == objectType.Id);

if (objectTypeDb is null)
if (objectTypeDb is null)
{
return null;
}

objectTypeDb.Name = objectType.Name;
_context.SaveChanges();

return objectType;
}

// TODO: Refator this method to use bool return type
public void DeleteObjectType(int objectTypeId)
public bool DeleteObjectType(int objectTypeId)
{
var objectType = _context.ObjectTypes.FirstOrDefault(r => r.Id == objectTypeId);

if ( objectType is null)
return;
if ( objectType is null)
{
return false;
}

_context.ObjectTypes.Remove(objectType);
_context.SaveChanges();

return true;
}

public ObjectType? GetObjectTypeById(int objectTypeId)
50 changes: 33 additions & 17 deletions Server/ReasnAPI/ReasnAPI/Services/ParticipantService.cs
Original file line number Diff line number Diff line change
@@ -10,21 +10,27 @@ public class ParticipantService(ReasnContext context)

public ParticipantDto? CreateParticipant(ParticipantDto? participantDto)
{
if (participantDto is null)
return null;
if (participantDto is null)
{
return null;
}

// check if participant already exists (same event and user)
var participantDb = _context.Participants.FirstOrDefault(r => r.Event.Id == participantDto.EventId && r.User.Id == participantDto.UserId);

if (participantDb is not null)
return null;
if (participantDb is not null)
{
return null;
}

var user = _context.Users.FirstOrDefault(r => r.Id == participantDto.UserId);
var eventInDb = _context.Events.FirstOrDefault(r => r.Id == participantDto.EventId); // event is a reserved keyword
var status = _context.Statuses.FirstOrDefault(r => r.Id == participantDto.StatusId);
var userInDb = _context.Users.FirstOrDefault(r => r.Id == participantDto.UserId);
var eventInDb = _context.Events.FirstOrDefault(r => r.Id == participantDto.EventId);
var statusInDb = _context.Statuses.FirstOrDefault(r => r.Id == participantDto.StatusId);

if (user is null || eventInDb is null || status is null)
return null;
if (userInDb is null || eventInDb is null || statusInDb is null)
{
return null;
}

var participant = new Participant
{
@@ -41,16 +47,22 @@ public class ParticipantService(ReasnContext context)

public ParticipantDto? UpdateParticipant(int participantId, ParticipantDto? participantDto)
{
if (participantDto is null)
return null;
if (participantDto is null)
{
return null;
}

var participant = _context.Participants.FirstOrDefault(r => r.Id == participantId);
if (participant is null)
return null;
if (participant is null)
{
return null;
}

var status = _context.Statuses.FirstOrDefault(r => r.Id == participantDto.StatusId);
if (status is null)
return null;
if (status is null)
{
return null;
}

participant.StatusId = participantDto.StatusId;

@@ -63,8 +75,10 @@ public bool DeleteParticipant(int participantId)
{
var participant = _context.Participants.FirstOrDefault(r => r.Id == participantId);

if (participant is null)
if (participant is null)
{
return false;
}

_context.Participants.Remove(participant);
_context.SaveChanges();
@@ -76,8 +90,10 @@ public bool DeleteParticipant(int participantId)
{
var participant = _context.Participants.FirstOrDefault(r => r.Id == participantId);

if (participant is null)
if (participant is null)
{
return null;
}

return MapToParticipantDto(participant);
}
26 changes: 19 additions & 7 deletions Server/ReasnAPI/ReasnAPI/Services/RoleService.cs
Original file line number Diff line number Diff line change
@@ -10,14 +10,18 @@ public class RoleService(ReasnContext context)

public RoleDto? CreateRole(RoleDto? roleDto)
{
if (roleDto is null)
if (roleDto is null)
{
return null;
}

// check if role with the same name already exists
var roleDb = _context.Roles.FirstOrDefault(r => r.Name == roleDto.Name);

if (roleDb is not null)
if (roleDb is not null)
{
return null;
}

var role = new Role
{
@@ -32,13 +36,17 @@ public class RoleService(ReasnContext context)

public RoleDto? UpdateRole(int roleId, RoleDto? roleDto)
{
if (roleDto is null)
if (roleDto is null)
{
return null;
}

var role = _context.Roles.FirstOrDefault(r => r.Id == roleId);

if (role is null)
return null;
{
return null;
}

role.Name = roleDto.Name;

@@ -51,8 +59,10 @@ public bool DeleteRole(int roleId)
{
var role = _context.Roles.FirstOrDefault(r => r.Id == roleId);

if (role is null)
if (role is null)
{
return false;
}

_context.Roles.Remove(role);
_context.SaveChanges();
@@ -64,8 +74,10 @@ public bool DeleteRole(int roleId)
{
var role = _context.Roles.FirstOrDefault(r => r.Id == roleId);

if (role is null)
return null;
if (role is null)
{
return null;
}

return MapToRoleDto(role);
}
Loading

0 comments on commit 1a58a27

Please sign in to comment.