Skip to content

Commit

Permalink
Updated services
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed May 7, 2024
1 parent 476e2d2 commit 58c94c8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
20 changes: 20 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Services/InterestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ public InterestDto UpdateInterest(int interestId, InterestDto interestDto)
context.SaveChanges();
return interestDto;
}

public bool DeleteInterest(int id)
{
var interest = context.Interests.FirstOrDefault(r => r.Id == id);
var eventInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id);
if (eventInterest != null)
{
return false;
}

if (interest == null)
{
Expand All @@ -49,6 +55,20 @@ public bool DeleteInterest(int id)
context.Interests.Remove(interest);
context.SaveChanges();

return true;
}

public bool DeleteInterestFromUserInterests(int interestId, int userId)
{
var userInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == interestId && r.UserId == userId);
if (userInterest == null)
{
return false;
}

context.UserInterests.Remove(userInterest);
context.SaveChanges();

return true;
}

Expand Down
4 changes: 2 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ParameterService(ReasnContext context)
{
public ParameterDto CreateParameter(ParameterDto parameterDto)
{
var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key);
if (parameter != null && parameter.Value == parameterDto.Value)
var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key && r.Value == parameterDto.Value);
if (parameter != null)
{
return null;
}
Expand Down
15 changes: 8 additions & 7 deletions Server/ReasnAPI/ReasnAPI/Services/StatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ public bool DeleteStatus(int statusId)
{
var status = context.Statuses.FirstOrDefault(r => r.Id == statusId);

var statusCheck = context.Participants.FirstOrDefault(r => r.StatusId == statusId);
var statusCheck2 = context.Events.FirstOrDefault(r => r.StatusId == statusId);

if (statusCheck != null ||
statusCheck2 != null) // if status is associated with a participant or event, it cannot be deleted
if (status == null)
{
return false;
}

if (status == null)
var statusAssignedToParticipant = context.Participants.FirstOrDefault(r => r.StatusId == statusId);
var statusAssignedToEvent = context.Events.FirstOrDefault(r => r.StatusId == statusId);

if (statusAssignedToParticipant != null ||
statusAssignedToEvent != null) // if status is associated with a participant or event, it cannot be deleted
{
return false;
}

context.Statuses.Remove(status);
context.SaveChanges();

Expand All @@ -67,7 +68,7 @@ public bool DeleteStatus(int statusId)

public StatusDto GetStatusById(int statusId)
{
var status = context.Statuses.FirstOrDefault(r => r.Id == statusId);
var status = context.Statuses.Find(statusId);
if (status == null)
{
return null;
Expand Down
59 changes: 44 additions & 15 deletions Server/ReasnAPI/ReasnAPI/Services/TagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ReasnAPI.Models.DTOs;
using System.Linq;
using System.Linq.Expressions;
using System.Transactions;

namespace ReasnAPI.Services;
public class TagService (ReasnContext context)
Expand All @@ -23,25 +24,53 @@ public TagDto CreateTag(TagDto tagDto)
return tagDto;
}

public TagDto UpdateTag(int tagId,TagDto tagDto)
public TagDto UpdateTag(int tagId, TagDto tagDto, int eventId)
{
var tag = context.Tags.FirstOrDefault(r => r.Id == tagId);

var eventTag = context.EventTags.FirstOrDefault(r => r.TagId == tagId);
if (eventTag != null) // if tag is associated with an event, it cannot be updated
using (var scope = new TransactionScope())
{
return null;
}
var tag = context.Tags.FirstOrDefault(r => r.Id == tagId);

if(tag == null)
{
return null;
if (tag == null)
{
return null;

Check warning on line 35 in Server/ReasnAPI/ReasnAPI/Services/TagService.cs

View workflow job for this annotation

GitHub Actions / tests

Possible null reference return.
}

var eventTags = context.EventTags.Where(r => r.TagId == tagId).ToList();
if (eventTags.Any(et => et.EventId != eventId)) // if tag is associated with more than one event
{
// Create new tag and event tag, and remove the old one
var newTag = new Tag
{
Name = tagDto.Name
};
context.Tags.Add(newTag);
context.SaveChanges();

var newEventTag = new EventTag
{
EventId = eventId,
TagId = newTag.Id
};
context.EventTags.Add(newEventTag);

var tagsToRemove = context.EventTags.Where(r => r.EventId == eventId && r.TagId == tagId).ToList();
context.EventTags.RemoveRange(tagsToRemove);
}
else if (eventTags.Count == 1 &&
eventTags[0].EventId == eventId) // if tag is associated only with the same event
{
tag.Name = tagDto.Name;
context.Tags.Update(tag);
}
else
{
return null;

Check warning on line 67 in Server/ReasnAPI/ReasnAPI/Services/TagService.cs

View workflow job for this annotation

GitHub Actions / tests

Possible null reference return.
}

context.SaveChanges();
return tagDto;
}
tag.Name = tagDto.Name;
context.Tags.Update(tag);
context.SaveChanges();
return tagDto;
}
}

public bool DeleteTag(int tagId)
{
Expand Down

0 comments on commit 58c94c8

Please sign in to comment.