From ea37bc7a1002bd5d52e77387f6121e6da58b82c5 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil <126111610+bilimig@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:41:13 +0100 Subject: [PATCH 01/65] Created CRUD services for Parameter, Status, Tag and C&U for Event --- .../ReasnAPI/Services/EventService.cs | 111 +++++++++++++++++- .../ReasnAPI/Services/ParameterService.cs | 100 ++++++++++++++++ .../ReasnAPI/ReasnAPI/Services/TagService.cs | 96 ++++++++++++++- 3 files changed, 304 insertions(+), 3 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 5f1be88a..7a75b8db 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -1,16 +1,123 @@ -using ReasnAPI.Models.Database; +using Microsoft.EntityFrameworkCore; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; namespace ReasnAPI.Services { public class EventService (ReasnContext context) { private readonly ReasnContext _context = context; /* TODO: Create following functions for this class - * create + * create (if one eventtag failed stoped the process) * update * delete * get by ID * get list by filter * get all */ + + public EventDto CreateEvent(EventDto eventDto) + { + + var newEvent = new Event + { + Name = eventDto.Name, + AddressId = eventDto.AddressId, + Description = eventDto.Description, + OrganizerId = eventDto.OrganizerId, + StartAt = eventDto.StartAt, + EndAt = eventDto.EndAt, + CreatedAt = eventDto.CreatedAt, + UpdatedAt = eventDto.UpdatedAt, + Slug = eventDto.Slug, + StatusId = eventDto.StatusId, + + }; + _context.Events.Add(newEvent); + _context.SaveChanges(); + var tempEvent = _context.Events.FirstOrDefault(r => r.Slug == eventDto.Slug); + int eventId = tempEvent.Id; + + foreach (var tag in eventDto.Tags) + { + var tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); + if(tempTag == null) + { + var newTag = new Tag + { + Name = tag.Name + }; + _context.Tags.Add(newTag); + _context.SaveChanges(); + tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); + } + + var eventTag = new EventTag + { + EventId = eventId, + TagId = tempTag.Id + }; + _context.EventTags.Add(eventTag); + _context.SaveChanges(); + } + + return eventDto; + + } + + public EventDto UpdateEvent(int eventId, EventDto eventDto) + { + var eventToUpdate = _context.Events.FirstOrDefault(r => r.Id == eventId); + if(eventToUpdate == null) + { + return null; + } + + eventToUpdate.Name = eventDto.Name; + eventToUpdate.AddressId = eventDto.AddressId; + eventToUpdate.Description = eventDto.Description; + eventToUpdate.OrganizerId = eventDto.OrganizerId; + eventToUpdate.StartAt = eventDto.StartAt; + eventToUpdate.EndAt = eventDto.EndAt; + eventToUpdate.CreatedAt = eventDto.CreatedAt; + eventToUpdate.UpdatedAt = eventDto.UpdatedAt; + eventToUpdate.Slug = eventDto.Slug; + eventToUpdate.StatusId = eventDto.StatusId; + + _context.Events.Update(eventToUpdate); + _context.SaveChanges(); + + + var existingTags = _context.EventTags.Where(et => et.EventId == eventId).ToList(); + + var newTags = eventDto.Tags.Select(tagDto => new EventTag + { + Tag = new Tag + { + Name = tagDto.Name, + } + }).ToList(); + + foreach (var existingTag in existingTags) + { + if (!newTags.Any(t => t.Tag == existingTag.Tag)) + { + _context.EventTags.Remove(existingTag); + } + } + + foreach (var newTag in newTags) + { + if (!existingTags.Any(t => t.Tag == newTag.Tag)) + { + _context.EventTags.Add(new EventTag { EventId = eventId, Tag = newTag.Tag }); + } + } + + return eventDto; + } + + + + } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 9c178c2f..1cdebcd0 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -1,4 +1,5 @@ using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; namespace ReasnAPI.Services { public class ParameterService (ReasnContext context){ @@ -12,5 +13,104 @@ public class ParameterService (ReasnContext context){ * get list by filter * get all */ + + public ParameterDto CreateParameter(ParameterDto parameterDto) + { + var parameter = new Parameter + { + Key = parameterDto.Key, + Value = parameterDto.Value + }; + + _context.Parameters.Add(parameter); + _context.SaveChanges(); + + return parameterDto; + } + + public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) + { + var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); + + if(parameter == null) + { + return null; + } + + parameter.Key = parameterDto.Key; + parameter.Value = parameterDto.Value; + + _context.Parameters.Update(parameter); + _context.SaveChanges(); + + return parameterDto; + } + + public bool DeleteParameter(int parameterId) + { + var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); + if(parameter == null) + { + return false; + } + + _context.Parameters.Remove(parameter); + _context.SaveChanges(); + + return true; + } + + public ParameterDto GetParameterById(int parameterId) + { + var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); + if(parameter == null) + { + return null; + } + + var parameterDto = new ParameterDto + { + Key = parameter.Key, + Value = parameter.Value + }; + + return parameterDto; + } + + public List GetAllParameters() + { + var parameters = _context.Parameters.ToList(); + var parameterDtos = new List(); + + foreach(var parameter in parameters) + { + parameterDtos.Add(new ParameterDto + { + Key = parameter.Key, + Value = parameter.Value + }); + } + + return parameterDtos; + } + + public List GetParametersByFilter(string key, string value) + { + var parameters = _context.Parameters.Where(r => r.Key == key && r.Value == value).ToList(); + var parameterDtos = new List(); + + foreach(var parameter in parameters) + { + parameterDtos.Add(new ParameterDto + { + Key = parameter.Key, + Value = parameter.Value + }); + } + + return parameterDtos; + } + + } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index a79b01c9..f3aac06f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -1,16 +1,110 @@ using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; namespace ReasnAPI.Services { public class TagService (ReasnContext context) { private readonly ReasnContext _context = context; /* TODO: Create following functions for this class - * create + * create (cheacking if already exist if not add) * update * delete * get by ID * get list by filter * get all */ + + public TagDto CreateTag(TagDto tagDto) + { + var tag = _context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); + if (tag == null) + { + var newTag = new Tag + { + Name = tagDto.Name + }; + + _context.Tags.Add(newTag); + _context.SaveChanges(); + } + + + return tagDto; + } + + + public TagDto UpdateTag(int tagId,TagDto tagDto) + { + var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); + if(tag == null) + { + return null; + } + tag.Name = tagDto.Name; + + _context.Tags.Update(tag); + _context.SaveChanges(); + + return tagDto; + } + + public bool DeleteTag(int tagId) + { + var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); + if(tag == null) + { + return false; + } + + _context.Tags.Remove(tag); + _context.SaveChanges(); + + return true; + } + + public TagDto GetTagById(int tagId) + { + var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); + if(tag == null) + { + return null; + } + + return new TagDto + { + Name = tag.Name + }; + } + + public List GetAllTags() + { + var tags = _context.Tags.ToList(); + var tagDtos = new List(); + foreach(var tag in tags) + { + tagDtos.Add(new TagDto + { + Name = tag.Name + }); + } + + return tagDtos; + } + + public List GetTagsByFilter(string filter) + { + var tags = _context.Tags.Where(r => r.Name.Contains(filter)).ToList(); + var tagDtos = new List(); + foreach(var tag in tags) + { + tagDtos.Add(new TagDto + { + Name = tag.Name + }); + } + + return tagDtos; + } + } } From bbe1798ecda2739d97303cf58c9d1e7d8a9e01cb Mon Sep 17 00:00:00 2001 From: Owczarek Kamil <126111610+bilimig@users.noreply.github.com> Date: Sat, 30 Mar 2024 20:27:03 +0100 Subject: [PATCH 02/65] created CRUD Image, Interest, Event and Parameter Services Also added geting all elements and by filter(not done properly yet) --- .../ReasnAPI/Services/EventService.cs | 146 ++++++++++++++++-- .../ReasnAPI/Services/ImageService.cs | 103 ++++++++++++ .../ReasnAPI/Services/IntrestService.cs | 99 ++++++++++++ .../ReasnAPI/Services/ParameterService.cs | 30 ++-- 4 files changed, 350 insertions(+), 28 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 7a75b8db..ef968c0d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -7,12 +7,12 @@ public class EventService (ReasnContext context) { private readonly ReasnContext _context = context; /* TODO: Create following functions for this class - * create (if one eventtag failed stoped the process) - * update - * delete - * get by ID - * get list by filter - * get all + * create (if one eventtag failed stoped the process) + * update (with eventtags) + * delete (with eventtags) + * get by ID (with eventtags) + * get list by filter (with eventtags) + * get all (with eventtags) */ public EventDto CreateEvent(EventDto eventDto) @@ -32,11 +32,12 @@ public EventDto CreateEvent(EventDto eventDto) StatusId = eventDto.StatusId, }; + _context.Events.Add(newEvent); _context.SaveChanges(); var tempEvent = _context.Events.FirstOrDefault(r => r.Slug == eventDto.Slug); int eventId = tempEvent.Id; - + if(eventDto.Tags != null) foreach (var tag in eventDto.Tags) { var tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); @@ -56,6 +57,7 @@ public EventDto CreateEvent(EventDto eventDto) EventId = eventId, TagId = tempTag.Id }; + _context.EventTags.Add(eventTag); _context.SaveChanges(); } @@ -87,7 +89,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) _context.SaveChanges(); - var existingTags = _context.EventTags.Where(et => et.EventId == eventId).ToList(); + var existingTags = _context.EventTags.Where(r => r.EventId == eventId).ToList(); var newTags = eventDto.Tags.Select(tagDto => new EventTag { @@ -99,7 +101,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) foreach (var existingTag in existingTags) { - if (!newTags.Any(t => t.Tag == existingTag.Tag)) + if (!newTags.Any(r => r.TagId == existingTag.TagId)) { _context.EventTags.Remove(existingTag); } @@ -107,16 +109,134 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) foreach (var newTag in newTags) { - if (!existingTags.Any(t => t.Tag == newTag.Tag)) + if (!existingTags.Any(r => r.TagId == newTag.TagId)) { - _context.EventTags.Add(new EventTag { EventId = eventId, Tag = newTag.Tag }); + _context.EventTags.Add(new EventTag { EventId = eventId, TagId = newTag.TagId }); } } return eventDto; } - - + + public bool DeleteEvent(int eventId) + { + var eventToDelete = _context.Events.FirstOrDefault(r => r.Id == eventId); + if(eventToDelete == null) + { + return false; + } + + _context.EventTags.RemoveRange(_context.EventTags.Where(r => r.EventId == eventId)); + _context.Events.Remove(eventToDelete); + + _context.SaveChanges(); + return true; + } + + public EventDto GetEventById(int eventId) + { + var eventToReturn = _context.Events.FirstOrDefault(r => r.Id == eventId); + if(eventToReturn == null) + { + return null; + } + + var eventTags = _context.EventTags.Where(r => r.EventId == eventId).ToList(); + var tags = new List(); + foreach (var eventTag in eventTags) + { + var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); + tags.Add(new TagDto { Name = tag.Name }); + } + + var eventDto = new EventDto + { + Name = eventToReturn.Name, + AddressId = eventToReturn.AddressId, + Description = eventToReturn.Description, + OrganizerId = eventToReturn.OrganizerId, + StartAt = eventToReturn.StartAt, + EndAt = eventToReturn.EndAt, + CreatedAt = eventToReturn.CreatedAt, + UpdatedAt = eventToReturn.UpdatedAt, + Slug = eventToReturn.Slug, + StatusId = eventToReturn.StatusId, + Tags = tags + }; + + return eventDto; + } + + public List GetEventsByFilter(string filter) + { + var events = _context.Events.Where(r => r.Name.Contains(filter)).ToList(); + var eventDtos = new List(); + foreach (var eventToReturn in events) + { + var eventTags = _context.EventTags.Where(r => r.EventId == eventToReturn.Id).ToList(); + var tags = new List(); + foreach (var eventTag in eventTags) + { + var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); + tags.Add(new TagDto { Name = tag.Name }); + } + + var eventDto = new EventDto + { + Name = eventToReturn.Name, + AddressId = eventToReturn.AddressId, + Description = eventToReturn.Description, + OrganizerId = eventToReturn.OrganizerId, + StartAt = eventToReturn.StartAt, + EndAt = eventToReturn.EndAt, + CreatedAt = eventToReturn.CreatedAt, + UpdatedAt = eventToReturn.UpdatedAt, + Slug = eventToReturn.Slug, + StatusId = eventToReturn.StatusId, + Tags = tags + }; + + eventDtos.Add(eventDto); + } + + return eventDtos; + } + + public List GetAllEvents() + { + var events = _context.Events.ToList(); + var eventDtos = new List(); + foreach (var eventToReturn in events) + { + var eventTags = _context.EventTags.Where(r => r.EventId == eventToReturn.Id).ToList(); + var tags = new List(); + foreach (var eventTag in eventTags) + { + var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); + tags.Add(new TagDto { Name = tag.Name }); + } + + var eventDto = new EventDto + { + Name = eventToReturn.Name, + AddressId = eventToReturn.AddressId, + Description = eventToReturn.Description, + OrganizerId = eventToReturn.OrganizerId, + StartAt = eventToReturn.StartAt, + EndAt = eventToReturn.EndAt, + CreatedAt = eventToReturn.CreatedAt, + UpdatedAt = eventToReturn.UpdatedAt, + Slug = eventToReturn.Slug, + StatusId = eventToReturn.StatusId, + Tags = tags + }; + + eventDtos.Add(eventDto); + } + + return eventDtos; + } + } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index e077a215..50d58a96 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -1,4 +1,5 @@ using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; namespace ReasnAPI.Services { public class ImageService (ReasnContext context) { @@ -12,5 +13,107 @@ public class ImageService (ReasnContext context) { * get list by filter * get all */ + + public ImageDto CreateImage(ImageDto imageDto) + { + var newImage = new Image + { + ImageData = imageDto.ImageData, + ObjectId = imageDto.ObjectId, + ObjectTypeId = imageDto.ObjectTypeId + }; + + _context.Images.Add(newImage); + _context.SaveChanges(); + return imageDto; + } + + public ImageDto UpdateImage(int imageId,ImageDto imageDto) + { + var image = _context.Images.FirstOrDefault(r => r.Id == imageId); + if (image == null) + { + return null; + } + + image.ObjectId = imageDto.ObjectId; + image.ImageData = imageDto.ImageData; + image.ObjectTypeId = imageDto.ObjectTypeId; + + _context.Images.Update(image); + _context.SaveChanges(); + return imageDto; + } + + public bool DeleteImage(int id) + { + var image = _context.Images.Find(id); + if (image == null) + { + return false; + } + + _context.Images.Remove(image); + _context.SaveChanges(); + return true; + } + + public ImageDto GetImageById(int id) + { + var image = _context.Images.Find(id); + if (image == null) + { + return null; + } + + var imageDto = new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }; + + return imageDto; + } + + public List GetImagesByFilter(int objectTypeId, int objectId) + { + var images = _context.Images.Where(r => r.ObjectTypeId == objectTypeId && r.ObjectId == objectId).ToList(); + if (images == null) + { + return null; + } + + var imageDtos = images.Select(image => new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }).ToList(); + + return imageDtos; + } + + public List GetAllImages() + { + var images = _context.Images.ToList(); + if (images == null) + { + return null; + } + + var imageDtos = images.Select(image => new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }).ToList(); + + return imageDtos; + } + + + + } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs index 5f2087fa..92cd4cc1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs @@ -1,4 +1,5 @@ using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; namespace ReasnAPI.Services { public class IntrestService (ReasnContext context) { @@ -12,5 +13,103 @@ public class IntrestService (ReasnContext context) { * get list by filter * get all */ + + public IntrestDto CreateIntrest(IntrestDto intrestDto) + { + var newIntrest = new Interest + { + Name = intrestDto.Name + }; + + _context.Interests.Add(newIntrest); + _context.SaveChanges(); + return intrestDto; + } + + + public IntrestDto UpdateIntrest(int intrestId,IntrestDto intrestDto) + { + var intrest = _context.Interests.FirstOrDefault(r => r.Id == intrestId); + if (intrest == null) + { + return null; + } + + intrest.Name = intrestDto.Name; + + _context.Interests.Update(intrest); + _context.SaveChanges(); + return intrestDto; + } + + public bool DeleteIntrest(int id) + { + var intrest = _context.Interests.Find(id); + if (intrest == null) + { + return false; + } + + _context.Interests.Remove(intrest); + _context.SaveChanges(); + return true; + } + + public IntrestDto GetIntrestById(int intrestId) + { + var intrest = _context.Interests.Find(intrestId); + if (intrest == null) + { + return null; + } + + var intrestDto = new IntrestDto + { + Name = intrest.Name + }; + + return intrestDto; + } + + public List GetIntrestsByFilter(string filter) + { + var intrests = _context.Interests.Where(r => r.Name.Contains(filter)).ToList(); + if (intrests == null) + { + return null; + } + + var intrestDtos = new List(); + foreach (var intrest in intrests) + { + intrestDtos.Add(new IntrestDto + { + Name = intrest.Name + }); + } + + return intrestDtos; + } + + public List GetAllIntrests() + { + var intrests = _context.Interests.ToList(); + var intrestDtos = new List(); + foreach (var intrest in intrests) + { + intrestDtos.Add(new IntrestDto + { + Name = intrest.Name + }); + } + + return intrestDtos; + } + + } + + + + } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 1cdebcd0..804adcfc 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -94,21 +94,21 @@ public List GetAllParameters() return parameterDtos; } - public List GetParametersByFilter(string key, string value) - { - var parameters = _context.Parameters.Where(r => r.Key == key && r.Value == value).ToList(); - var parameterDtos = new List(); - - foreach(var parameter in parameters) - { - parameterDtos.Add(new ParameterDto - { - Key = parameter.Key, - Value = parameter.Value - }); - } - - return parameterDtos; + public List GetParametersByFilter(string filter) + { + var parameters = _context.Parameters.Where(r => r.Key.Contains(filter)).ToList(); + var parameterDtos = new List(); + + foreach(var parameter in parameters) + { + parameterDtos.Add(new ParameterDto + { + Key = parameter.Key, + Value = parameter.Value + }); + } + + return parameterDtos; } From 9b0381fc5b5485c090ef37a42f1ef5101cbc50c2 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Tue, 2 Apr 2024 18:09:34 +0200 Subject: [PATCH 03/65] changed delete services to void isnted of bool --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 9 +++------ Server/ReasnAPI/ReasnAPI/Services/ImageService.cs | 10 +++------- Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs | 10 +++------- Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs | 10 ++++------ Server/ReasnAPI/ReasnAPI/Services/TagService.cs | 9 +++------ 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index ef968c0d..31a4c128 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -118,19 +118,16 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) return eventDto; } - public bool DeleteEvent(int eventId) + public void DeleteEvent(int eventId) { var eventToDelete = _context.Events.FirstOrDefault(r => r.Id == eventId); - if(eventToDelete == null) - { - return false; - } + _context.EventTags.RemoveRange(_context.EventTags.Where(r => r.EventId == eventId)); _context.Events.Remove(eventToDelete); _context.SaveChanges(); - return true; + } public EventDto GetEventById(int eventId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 50d58a96..a77e8b3c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -45,17 +45,13 @@ public ImageDto UpdateImage(int imageId,ImageDto imageDto) return imageDto; } - public bool DeleteImage(int id) + public void DeleteImage(int id) { var image = _context.Images.Find(id); - if (image == null) - { - return false; - } - + _context.Images.Remove(image); _context.SaveChanges(); - return true; + } public ImageDto GetImageById(int id) diff --git a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs index 92cd4cc1..2863cfcb 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs @@ -42,17 +42,13 @@ public IntrestDto UpdateIntrest(int intrestId,IntrestDto intrestDto) return intrestDto; } - public bool DeleteIntrest(int id) + public void DeleteIntrest(int id) { var intrest = _context.Interests.Find(id); - if (intrest == null) - { - return false; - } - + _context.Interests.Remove(intrest); _context.SaveChanges(); - return true; + } public IntrestDto GetIntrestById(int intrestId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 804adcfc..bb7ac558 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -46,18 +46,16 @@ public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) return parameterDto; } - public bool DeleteParameter(int parameterId) + public void DeleteParameter(int parameterId) { var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); - if(parameter == null) - { - return false; - } + + _context.Parameters.Remove(parameter); _context.SaveChanges(); - return true; + } public ParameterDto GetParameterById(int parameterId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index f3aac06f..1952abb0 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -48,18 +48,15 @@ public TagDto UpdateTag(int tagId,TagDto tagDto) return tagDto; } - public bool DeleteTag(int tagId) + public void DeleteTag(int tagId) { var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); - if(tag == null) - { - return false; - } + _context.Tags.Remove(tag); _context.SaveChanges(); - return true; + } public TagDto GetTagById(int tagId) From e101e093b4bd2688b3b10358f0a5bff6ab078a10 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 3 Apr 2024 20:45:49 +0200 Subject: [PATCH 04/65] Fixed GetByFilter service in Event, Image, Intrest, Parameter, Status and Tag Services --- .../ReasnAPI/Services/EventService.cs | 5 +-- .../ReasnAPI/Services/ImageService.cs | 14 +++++--- .../ReasnAPI/Services/IntrestService.cs | 32 +++++++++---------- .../ReasnAPI/Services/ParameterService.cs | 10 +++--- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 14 ++++---- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 31a4c128..eb376f51 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; +using System.Linq.Expressions; namespace ReasnAPI.Services { public class EventService (ReasnContext context) { @@ -164,9 +165,9 @@ public EventDto GetEventById(int eventId) return eventDto; } - public List GetEventsByFilter(string filter) + public List GetEventsByFilter(Expression> filter) { - var events = _context.Events.Where(r => r.Name.Contains(filter)).ToList(); + var events = _context.Events.Where(filter).ToList(); var eventDtos = new List(); foreach (var eventToReturn in events) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index a77e8b3c..a191eee2 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -1,5 +1,6 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; +using System.Linq.Expressions; namespace ReasnAPI.Services { public class ImageService (ReasnContext context) { @@ -70,11 +71,14 @@ public ImageDto GetImageById(int id) }; return imageDto; - } + } + + + - public List GetImagesByFilter(int objectTypeId, int objectId) + public List GetAllImages() { - var images = _context.Images.Where(r => r.ObjectTypeId == objectTypeId && r.ObjectId == objectId).ToList(); + var images = _context.Images.ToList(); if (images == null) { return null; @@ -90,9 +94,9 @@ public List GetImagesByFilter(int objectTypeId, int objectId) return imageDtos; } - public List GetAllImages() + public List GetImagesByFilter(Expression> filter) { - var images = _context.Images.ToList(); + var images = _context.Images.Where(filter).ToList(); if (images == null) { return null; diff --git a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs index 2863cfcb..0b2cceed 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs @@ -1,5 +1,6 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; +using System.Linq.Expressions; namespace ReasnAPI.Services { public class IntrestService (ReasnContext context) { @@ -67,14 +68,10 @@ public IntrestDto GetIntrestById(int intrestId) return intrestDto; } - public List GetIntrestsByFilter(string filter) + + public List GetAllIntrests() { - var intrests = _context.Interests.Where(r => r.Name.Contains(filter)).ToList(); - if (intrests == null) - { - return null; - } - + var intrests = _context.Interests.ToList(); var intrestDtos = new List(); foreach (var intrest in intrests) { @@ -87,25 +84,26 @@ public List GetIntrestsByFilter(string filter) return intrestDtos; } - public List GetAllIntrests() + public List GetIntrestsByFilter(Expression> filter) { - var intrests = _context.Interests.ToList(); - var intrestDtos = new List(); - foreach (var intrest in intrests) + var intrests = _context.Interests.Where(filter).ToList(); + if (intrests == null) { - intrestDtos.Add(new IntrestDto - { - Name = intrest.Name - }); + return null; } + var intrestDtos = intrests.Select(intrest => new IntrestDto + { + Name = intrest.Name + }).ToList(); + return intrestDtos; } + + } - - } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index bb7ac558..f9153571 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -1,5 +1,6 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; +using System.Linq.Expressions; namespace ReasnAPI.Services { public class ParameterService (ReasnContext context){ @@ -92,9 +93,10 @@ public List GetAllParameters() return parameterDtos; } - public List GetParametersByFilter(string filter) + + public List GetParametersByFilter(Expression> filter) { - var parameters = _context.Parameters.Where(r => r.Key.Contains(filter)).ToList(); + var parameters = _context.Parameters.Where(filter).ToList(); var parameterDtos = new List(); foreach(var parameter in parameters) @@ -107,8 +109,8 @@ public List GetParametersByFilter(string filter) } return parameterDtos; + } - - + } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 1952abb0..7e43d42d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -1,5 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; +using System.Linq; +using System.Linq.Expressions; namespace ReasnAPI.Services { public class TagService (ReasnContext context) { @@ -88,19 +90,19 @@ public List GetAllTags() return tagDtos; } - public List GetTagsByFilter(string filter) + public List GetTagsByFilter(Expression> filter) { - var tags = _context.Tags.Where(r => r.Name.Contains(filter)).ToList(); - var tagDtos = new List(); - foreach(var tag in tags) + var tags = _context.Tags.Where(filter).ToList(); + var tagsDtos = new List(); + foreach (var tag in tags) { - tagDtos.Add(new TagDto + tagsDtos.Add(new TagDto { Name = tag.Name }); } - return tagDtos; + return tagsDtos; } } From 690a93c9adde92f8e4547ee0bf6494afffdd7424 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sat, 13 Apr 2024 11:25:16 +0200 Subject: [PATCH 05/65] little changes --- Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj | 1 - .../ReasnAPI/Services/EventService.cs | 115 +++++++++--------- .../ReasnAPI/Services/ImageService.cs | 30 +---- .../ReasnAPI/Services/InterestService.cs | 89 ++++++++++++++ .../ReasnAPI/Services/IntrestService.cs | 109 ----------------- .../ReasnAPI/Services/ParameterService.cs | 21 +--- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 36 ++---- 7 files changed, 164 insertions(+), 237 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI/Services/InterestService.cs delete mode 100644 Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs diff --git a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj index 23499ae8..f3bbd683 100644 --- a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj +++ b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj @@ -24,7 +24,6 @@ - diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index eb376f51..b3b25f5e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -6,64 +6,59 @@ namespace ReasnAPI.Services { public class EventService (ReasnContext context) { private readonly ReasnContext _context = context; - - /* TODO: Create following functions for this class - * create (if one eventtag failed stoped the process) - * update (with eventtags) - * delete (with eventtags) - * get by ID (with eventtags) - * get list by filter (with eventtags) - * get all (with eventtags) - */ - public EventDto CreateEvent(EventDto eventDto) { - - var newEvent = new Event - { - Name = eventDto.Name, - AddressId = eventDto.AddressId, - Description = eventDto.Description, - OrganizerId = eventDto.OrganizerId, - StartAt = eventDto.StartAt, - EndAt = eventDto.EndAt, - CreatedAt = eventDto.CreatedAt, - UpdatedAt = eventDto.UpdatedAt, - Slug = eventDto.Slug, - StatusId = eventDto.StatusId, - - }; - - _context.Events.Add(newEvent); - _context.SaveChanges(); - var tempEvent = _context.Events.FirstOrDefault(r => r.Slug == eventDto.Slug); - int eventId = tempEvent.Id; - if(eventDto.Tags != null) - foreach (var tag in eventDto.Tags) + if (eventDto.Slug != null) { - var tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); - if(tempTag == null) + var newEvent = new Event { - var newTag = new Tag - { - Name = tag.Name - }; - _context.Tags.Add(newTag); - _context.SaveChanges(); - tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); - } - - var eventTag = new EventTag - { - EventId = eventId, - TagId = tempTag.Id + Name = eventDto.Name, + AddressId = eventDto.AddressId, + Description = eventDto.Description, + OrganizerId = eventDto.OrganizerId, + StartAt = eventDto.StartAt, + EndAt = eventDto.EndAt, + CreatedAt = eventDto.CreatedAt, + UpdatedAt = eventDto.UpdatedAt, + Slug = eventDto.Slug, + StatusId = eventDto.StatusId, }; - _context.EventTags.Add(eventTag); - _context.SaveChanges(); + _context.Events.Add(newEvent); + } + + _context.SaveChanges(); + var tempEvent = _context.Events.FirstOrDefault(r => r.Slug == eventDto.Slug); + if (tempEvent == null) return eventDto; + { + var eventId = tempEvent.Id; + if (eventDto.Tags == null) return eventDto; + foreach (var tag in eventDto.Tags) + { + var tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); + if(tempTag == null) + { + var newTag = new Tag + { + Name = tag.Name + }; + _context.Tags.Add(newTag); + _context.SaveChanges(); + tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); + } + + var eventTag = new EventTag + { + EventId = eventId, + TagId = tempTag.Id + }; + + _context.EventTags.Add(eventTag); + _context.SaveChanges(); + } } - return eventDto; + return eventDto; } @@ -102,7 +97,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) foreach (var existingTag in existingTags) { - if (!newTags.Any(r => r.TagId == existingTag.TagId)) + if (newTags.All(r => r.TagId != existingTag.TagId)) { _context.EventTags.Remove(existingTag); } @@ -110,7 +105,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) foreach (var newTag in newTags) { - if (!existingTags.Any(r => r.TagId == newTag.TagId)) + if (existingTags.All(r => r.TagId != newTag.TagId)) { _context.EventTags.Add(new EventTag { EventId = eventId, TagId = newTag.TagId }); } @@ -122,13 +117,13 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) public void DeleteEvent(int eventId) { var eventToDelete = _context.Events.FirstOrDefault(r => r.Id == eventId); - - - _context.EventTags.RemoveRange(_context.EventTags.Where(r => r.EventId == eventId)); - _context.Events.Remove(eventToDelete); + if (eventToDelete != null) + { + _context.EventTags.RemoveRange(_context.EventTags.Where(r => r.EventId == eventId)); + _context.Events.Remove(eventToDelete); + } _context.SaveChanges(); - } public EventDto GetEventById(int eventId) @@ -144,7 +139,7 @@ public EventDto GetEventById(int eventId) foreach (var eventTag in eventTags) { var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); - tags.Add(new TagDto { Name = tag.Name }); + if (tag != null) tags.Add(new TagDto { Name = tag.Name }); } var eventDto = new EventDto @@ -176,7 +171,7 @@ public List GetEventsByFilter(Expression> filter) foreach (var eventTag in eventTags) { var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); - tags.Add(new TagDto { Name = tag.Name }); + if (tag != null) tags.Add(new TagDto { Name = tag.Name }); } var eventDto = new EventDto @@ -211,7 +206,7 @@ public List GetAllEvents() foreach (var eventTag in eventTags) { var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); - tags.Add(new TagDto { Name = tag.Name }); + if (tag != null) tags.Add(new TagDto { Name = tag.Name }); } var eventDto = new EventDto diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index a191eee2..32febed1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -6,15 +6,6 @@ namespace ReasnAPI.Services { public class ImageService (ReasnContext context) { private readonly ReasnContext _context = context; - /* TODO: Create following functions for this class - * create - * update - * delete - * get by ID - * get list by filter - * get all - */ - public ImageDto CreateImage(ImageDto imageDto) { var newImage = new Image @@ -49,7 +40,7 @@ public ImageDto UpdateImage(int imageId,ImageDto imageDto) public void DeleteImage(int id) { var image = _context.Images.Find(id); - + if (image == null) { return;} _context.Images.Remove(image); _context.SaveChanges(); @@ -71,19 +62,12 @@ public ImageDto GetImageById(int id) }; return imageDto; - } - - - + } public List GetAllImages() { var images = _context.Images.ToList(); - if (images == null) - { - return null; - } - + var imageDtos = images.Select(image => new ImageDto { ImageData = image.ImageData, @@ -97,10 +81,6 @@ public List GetAllImages() public List GetImagesByFilter(Expression> filter) { var images = _context.Images.Where(filter).ToList(); - if (images == null) - { - return null; - } var imageDtos = images.Select(image => new ImageDto { @@ -111,9 +91,5 @@ public List GetImagesByFilter(Expression> filter) return imageDtos; } - - - - } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs new file mode 100644 index 00000000..b52525e8 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -0,0 +1,89 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using System.Linq.Expressions; + +namespace ReasnAPI.Services { + public class InterestService (ReasnContext context) { + private readonly ReasnContext _context = context; + + public InterestDto CreateInterest(InterestDto interestDto) + { + var newInterest = new Interest + { + Name = interestDto.Name + }; + + _context.Interests.Add(newInterest); + _context.SaveChanges(); + return interestDto; + } + + public InterestDto UpdateInterest(int interestId,InterestDto interestDto) + { + var interest = _context.Interests.FirstOrDefault(r => r.Id == interestId); + if (interest == null) + { + return null; + } + + interest.Name = interestDto.Name; + + _context.Interests.Update(interest); + _context.SaveChanges(); + return interestDto; + } + + public void DeleteInterest(int id) + { + var interest = _context.Interests.Find(id); + + _context.Interests.Remove(interest); + _context.SaveChanges(); + + } + + public InterestDto GetInterestById(int interestId) + { + var interest = _context.Interests.Find(interestId); + if (interest == null) + { + return null; + } + + var interestDto = new InterestDto + { + Name = interest.Name + }; + + return intrestDto; + } + + public List GetAllInterests() + { + var interests = _context.Interests.ToList(); + var interestDtos = new List(); + foreach (var intrest in interests) + { + interestDtos.Add(new InterestDto + { + Name = intrest.Name + }); + } + + return interestDtos; + } + + public List GetInterestsByFilter(Expression> filter) + { + var interests = _context.Interests.Where(filter).ToList(); + + var interestDtos = interests.Select(interest => new InterestDto + { + Name = interest.Name + }).ToList(); + + return interestDtos; + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs b/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs deleted file mode 100644 index 0b2cceed..00000000 --- a/Server/ReasnAPI/ReasnAPI/Services/IntrestService.cs +++ /dev/null @@ -1,109 +0,0 @@ -using ReasnAPI.Models.Database; -using ReasnAPI.Models.DTOs; -using System.Linq.Expressions; - -namespace ReasnAPI.Services { - public class IntrestService (ReasnContext context) { - private readonly ReasnContext _context = context; - - /* TODO: Create following functions for this class - * create - * update - * delete - * get by ID - * get list by filter - * get all - */ - - public IntrestDto CreateIntrest(IntrestDto intrestDto) - { - var newIntrest = new Interest - { - Name = intrestDto.Name - }; - - _context.Interests.Add(newIntrest); - _context.SaveChanges(); - return intrestDto; - } - - - public IntrestDto UpdateIntrest(int intrestId,IntrestDto intrestDto) - { - var intrest = _context.Interests.FirstOrDefault(r => r.Id == intrestId); - if (intrest == null) - { - return null; - } - - intrest.Name = intrestDto.Name; - - _context.Interests.Update(intrest); - _context.SaveChanges(); - return intrestDto; - } - - public void DeleteIntrest(int id) - { - var intrest = _context.Interests.Find(id); - - _context.Interests.Remove(intrest); - _context.SaveChanges(); - - } - - public IntrestDto GetIntrestById(int intrestId) - { - var intrest = _context.Interests.Find(intrestId); - if (intrest == null) - { - return null; - } - - var intrestDto = new IntrestDto - { - Name = intrest.Name - }; - - return intrestDto; - } - - - public List GetAllIntrests() - { - var intrests = _context.Interests.ToList(); - var intrestDtos = new List(); - foreach (var intrest in intrests) - { - intrestDtos.Add(new IntrestDto - { - Name = intrest.Name - }); - } - - return intrestDtos; - } - - public List GetIntrestsByFilter(Expression> filter) - { - var intrests = _context.Interests.Where(filter).ToList(); - if (intrests == null) - { - return null; - } - - var intrestDtos = intrests.Select(intrest => new IntrestDto - { - Name = intrest.Name - }).ToList(); - - return intrestDtos; - } - - - - - } - - -} diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index f9153571..75b2a42c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -6,15 +6,6 @@ namespace ReasnAPI.Services { public class ParameterService (ReasnContext context){ private readonly ReasnContext _context = context; - /* TODO: Create following functions for this class - * create - * update - * delete - * get by ID - * get list by filter - * get all - */ - public ParameterDto CreateParameter(ParameterDto parameterDto) { var parameter = new Parameter @@ -40,23 +31,20 @@ public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) parameter.Key = parameterDto.Key; parameter.Value = parameterDto.Value; - _context.Parameters.Update(parameter); _context.SaveChanges(); - return parameterDto; } public void DeleteParameter(int parameterId) { var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); - - - + if (parameter == null) + { + return; + } _context.Parameters.Remove(parameter); _context.SaveChanges(); - - } public ParameterDto GetParameterById(int parameterId) @@ -93,7 +81,6 @@ public List GetAllParameters() return parameterDtos; } - public List GetParametersByFilter(Expression> filter) { var parameters = _context.Parameters.Where(filter).ToList(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 7e43d42d..fa69689a 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -6,30 +6,20 @@ namespace ReasnAPI.Services { public class TagService (ReasnContext context) { private readonly ReasnContext _context = context; - - /* TODO: Create following functions for this class - * create (cheacking if already exist if not add) - * update - * delete - * get by ID - * get list by filter - * get all - */ - + public TagDto CreateTag(TagDto tagDto) { var tag = _context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); - if (tag == null) + if (tag != null) return tagDto; + + var newTag = new Tag { - var newTag = new Tag - { - Name = tagDto.Name - }; + Name = tagDto.Name + }; + + _context.Tags.Add(newTag); + _context.SaveChanges(); - _context.Tags.Add(newTag); - _context.SaveChanges(); - } - return tagDto; } @@ -53,12 +43,12 @@ public TagDto UpdateTag(int tagId,TagDto tagDto) public void DeleteTag(int tagId) { var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); - - + if (tag == null) + { + return; + } _context.Tags.Remove(tag); _context.SaveChanges(); - - } public TagDto GetTagById(int tagId) From 4332abe82339dd638c1bc65e18458a99dceb97a5 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sat, 13 Apr 2024 15:49:51 +0200 Subject: [PATCH 06/65] fixing build issue and changing list to ienumerable --- .../ReasnAPI/Services/EventService.cs | 110 +++++++++--------- .../ReasnAPI/Services/ImageService.cs | 4 +- .../ReasnAPI/Services/InterestService.cs | 22 ++-- .../ReasnAPI/Services/ParameterService.cs | 35 +----- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 30 +---- 5 files changed, 74 insertions(+), 127 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index b3b25f5e..e1fc9dcb 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -2,64 +2,60 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; - +using System.Linq; + namespace ReasnAPI.Services { public class EventService (ReasnContext context) { private readonly ReasnContext _context = context; public EventDto CreateEvent(EventDto eventDto) { - if (eventDto.Slug != null) + if (eventDto.Slug == null) return null; + + var newEvent = new Event { - var newEvent = new Event - { - Name = eventDto.Name, - AddressId = eventDto.AddressId, - Description = eventDto.Description, - OrganizerId = eventDto.OrganizerId, - StartAt = eventDto.StartAt, - EndAt = eventDto.EndAt, - CreatedAt = eventDto.CreatedAt, - UpdatedAt = eventDto.UpdatedAt, - Slug = eventDto.Slug, - StatusId = eventDto.StatusId, - }; - - _context.Events.Add(newEvent); - } - + Name = eventDto.Name, + AddressId = eventDto.AddressId, + Description = eventDto.Description, + OrganizerId = eventDto.OrganizerId, + StartAt = eventDto.StartAt, + EndAt = eventDto.EndAt, + CreatedAt = eventDto.CreatedAt, + UpdatedAt = eventDto.UpdatedAt, + Slug = eventDto.Slug, + StatusId = eventDto.StatusId, + }; + + _context.Events.Add(newEvent); _context.SaveChanges(); var tempEvent = _context.Events.FirstOrDefault(r => r.Slug == eventDto.Slug); if (tempEvent == null) return eventDto; + + var eventId = tempEvent.Id; + if (eventDto.Tags == null) {return eventDto;} + + foreach (var (tag, tempTag) in from tag in eventDto.Tags + let tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name) + select (tag, tempTag)) { - var eventId = tempEvent.Id; - if (eventDto.Tags == null) return eventDto; - foreach (var tag in eventDto.Tags) + if (tempTag == null) { - var tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); - if(tempTag == null) - { - var newTag = new Tag - { - Name = tag.Name - }; - _context.Tags.Add(newTag); - _context.SaveChanges(); - tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name); - } - - var eventTag = new EventTag - { - EventId = eventId, - TagId = tempTag.Id - }; - - _context.EventTags.Add(eventTag); - _context.SaveChanges(); + return null; } - } - + + var newTag = new Tag {Name = tag.Name }; + _context.Tags.Add(newTag); + _context.SaveChanges(); + + var eventTag = new EventTag + { + EventId = eventId, + TagId = tempTag.Id + }; + _context.EventTags.Add(eventTag); + _context.SaveChanges(); + } + return eventDto; - } public EventDto UpdateEvent(int eventId, EventDto eventDto) @@ -84,7 +80,6 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) _context.Events.Update(eventToUpdate); _context.SaveChanges(); - var existingTags = _context.EventTags.Where(r => r.EventId == eventId).ToList(); var newTags = eventDto.Tags.Select(tagDto => new EventTag @@ -96,19 +91,20 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) }).ToList(); foreach (var existingTag in existingTags) - { - if (newTags.All(r => r.TagId != existingTag.TagId)) - { - _context.EventTags.Remove(existingTag); + { + if (!newTags.Any(r => r.TagId == existingTag.TagId)) + { + _context.EventTags.Remove(existingTag); } } foreach (var newTag in newTags) - { - if (existingTags.All(r => r.TagId != newTag.TagId)) - { - _context.EventTags.Add(new EventTag { EventId = eventId, TagId = newTag.TagId }); - } + { + if (!existingTags.Any(r => r.TagId == newTag.TagId)) + { + + _context.EventTags.Add(new EventTag { EventId = eventId, TagId = newTag.TagId }); + } } return eventDto; @@ -160,7 +156,7 @@ public EventDto GetEventById(int eventId) return eventDto; } - public List GetEventsByFilter(Expression> filter) + public IEnumerable GetEventsByFilter(Expression> filter) { var events = _context.Events.Where(filter).ToList(); var eventDtos = new List(); @@ -195,7 +191,7 @@ public List GetEventsByFilter(Expression> filter) return eventDtos; } - public List GetAllEvents() + public IEnumerable GetAllEvents() { var events = _context.Events.ToList(); var eventDtos = new List(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 32febed1..098fb775 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -64,7 +64,7 @@ public ImageDto GetImageById(int id) return imageDto; } - public List GetAllImages() + public IEnumerable GetAllImages() { var images = _context.Images.ToList(); @@ -78,7 +78,7 @@ public List GetAllImages() return imageDtos; } - public List GetImagesByFilter(Expression> filter) + public IEnumerable GetImagesByFilter(Expression> filter) { var images = _context.Images.Where(filter).ToList(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index b52525e8..5cd43ae1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -36,7 +36,11 @@ public InterestDto UpdateInterest(int interestId,InterestDto interestDto) public void DeleteInterest(int id) { var interest = _context.Interests.Find(id); - + + if (interest == null) + { + return; + } _context.Interests.Remove(interest); _context.SaveChanges(); @@ -55,25 +59,17 @@ public InterestDto GetInterestById(int interestId) Name = interest.Name }; - return intrestDto; + return interestDto; } - public List GetAllInterests() + public IEnumerable GetAllInterests() { var interests = _context.Interests.ToList(); - var interestDtos = new List(); - foreach (var intrest in interests) - { - interestDtos.Add(new InterestDto - { - Name = intrest.Name - }); - } - return interestDtos; + return interests.Select(interest => new InterestDto { Name = interest.Name }).ToList(); } - public List GetInterestsByFilter(Expression> filter) + public IEnumerable GetInterestsByFilter(Expression> filter) { var interests = _context.Interests.Where(filter).ToList(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 75b2a42c..76c9a7e5 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -13,10 +13,8 @@ public ParameterDto CreateParameter(ParameterDto parameterDto) Key = parameterDto.Key, Value = parameterDto.Value }; - _context.Parameters.Add(parameter); _context.SaveChanges(); - return parameterDto; } @@ -28,7 +26,6 @@ public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) { return null; } - parameter.Key = parameterDto.Key; parameter.Value = parameterDto.Value; _context.Parameters.Update(parameter); @@ -64,38 +61,18 @@ public ParameterDto GetParameterById(int parameterId) return parameterDto; } - public List GetAllParameters() + public IEnumerable GetAllParameters() { var parameters = _context.Parameters.ToList(); - var parameterDtos = new List(); - foreach(var parameter in parameters) - { - parameterDtos.Add(new ParameterDto - { - Key = parameter.Key, - Value = parameter.Value - }); - } - - return parameterDtos; + return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); } - public List GetParametersByFilter(Expression> filter) + public IEnumerable GetParametersByFilter(Expression> filter) { - var parameters = _context.Parameters.Where(filter).ToList(); - var parameterDtos = new List(); - - foreach(var parameter in parameters) - { - parameterDtos.Add(new ParameterDto - { - Key = parameter.Key, - Value = parameter.Value - }); - } - - return parameterDtos; + var parameters = _context.Parameters.Where(filter).ToList(); + + return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index fa69689a..6995cd9b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -16,14 +16,10 @@ public TagDto CreateTag(TagDto tagDto) { Name = tagDto.Name }; - _context.Tags.Add(newTag); _context.SaveChanges(); - - return tagDto; } - public TagDto UpdateTag(int tagId,TagDto tagDto) { @@ -33,10 +29,8 @@ public TagDto UpdateTag(int tagId,TagDto tagDto) return null; } tag.Name = tagDto.Name; - _context.Tags.Update(tag); _context.SaveChanges(); - return tagDto; } @@ -65,34 +59,18 @@ public TagDto GetTagById(int tagId) }; } - public List GetAllTags() + public IEnumerable GetAllTags() { var tags = _context.Tags.ToList(); - var tagDtos = new List(); - foreach(var tag in tags) - { - tagDtos.Add(new TagDto - { - Name = tag.Name - }); - } - return tagDtos; + return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); } - public List GetTagsByFilter(Expression> filter) + public IEnumerable GetTagsByFilter(Expression> filter) { var tags = _context.Tags.Where(filter).ToList(); - var tagsDtos = new List(); - foreach (var tag in tags) - { - tagsDtos.Add(new TagDto - { - Name = tag.Name - }); - } - return tagsDtos; + return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); } } From a7495d1d83cda182880243ef9dfd897a3eee692f Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sun, 14 Apr 2024 14:42:48 +0200 Subject: [PATCH 07/65] Fixing EventServices and standardizing syntax issues --- .../Services/TagServiceTests.cs | 17 + .../ReasnAPI/Services/EventService.cs | 363 +++++++++--------- .../ReasnAPI/Services/ImageService.cs | 141 ++++--- .../ReasnAPI/Services/InterestService.cs | 122 +++--- .../ReasnAPI/Services/ParameterService.cs | 113 +++--- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 106 +++-- 6 files changed, 445 insertions(+), 417 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs new file mode 100644 index 00000000..9bf6b13f --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; + + +namespace ReasnAPI.Tests.Services +{ + internal class TagServiceTests + { + + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index e1fc9dcb..399c6f86 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -3,139 +3,188 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using System.Linq; +using System.Transactions; + +namespace ReasnAPI.Services; +public class EventService (ReasnContext context) +{ + public EventDto CreateEvent(EventDto eventDto) + { + using (var scope = new TransactionScope()) + { + eventDto.Slug = CreateSlug(eventDto); -namespace ReasnAPI.Services { - public class EventService (ReasnContext context) { - private readonly ReasnContext _context = context; - public EventDto CreateEvent(EventDto eventDto) - { - if (eventDto.Slug == null) return null; - - var newEvent = new Event - { - Name = eventDto.Name, - AddressId = eventDto.AddressId, - Description = eventDto.Description, - OrganizerId = eventDto.OrganizerId, - StartAt = eventDto.StartAt, - EndAt = eventDto.EndAt, - CreatedAt = eventDto.CreatedAt, - UpdatedAt = eventDto.UpdatedAt, - Slug = eventDto.Slug, + var newEvent = new Event + { + Name = eventDto.Name, + AddressId = eventDto.AddressId, + Description = eventDto.Description, + OrganizerId = eventDto.OrganizerId, + StartAt = eventDto.StartAt, + EndAt = eventDto.EndAt, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = eventDto.Slug, StatusId = eventDto.StatusId, - }; - - _context.Events.Add(newEvent); - _context.SaveChanges(); - var tempEvent = _context.Events.FirstOrDefault(r => r.Slug == eventDto.Slug); - if (tempEvent == null) return eventDto; + }; - var eventId = tempEvent.Id; - if (eventDto.Tags == null) {return eventDto;} + context.Events.Add(newEvent); + context.SaveChanges(); - foreach (var (tag, tempTag) in from tag in eventDto.Tags - let tempTag = _context.Tags.FirstOrDefault(r => r.Name == tag.Name) - select (tag, tempTag)) + var eventId = newEvent.Id; + if (eventDto.Tags == null) { - if (tempTag == null) - { - return null; - } + return eventDto; + } + + foreach (var tag in eventDto.Tags) + { + var newTag = new Tag { Name = tag.Name }; + context.Tags.Add(newTag); + context.SaveChanges(); - var newTag = new Tag {Name = tag.Name }; - _context.Tags.Add(newTag); - _context.SaveChanges(); - var eventTag = new EventTag { EventId = eventId, - TagId = tempTag.Id + TagId = newTag.Id }; - _context.EventTags.Add(eventTag); - _context.SaveChanges(); + context.EventTags.Add(eventTag); + context.SaveChanges(); } + } - return eventDto; - } - - public EventDto UpdateEvent(int eventId, EventDto eventDto) - { - var eventToUpdate = _context.Events.FirstOrDefault(r => r.Id == eventId); - if(eventToUpdate == null) - { - return null; - } - - eventToUpdate.Name = eventDto.Name; - eventToUpdate.AddressId = eventDto.AddressId; - eventToUpdate.Description = eventDto.Description; - eventToUpdate.OrganizerId = eventDto.OrganizerId; - eventToUpdate.StartAt = eventDto.StartAt; - eventToUpdate.EndAt = eventDto.EndAt; - eventToUpdate.CreatedAt = eventDto.CreatedAt; - eventToUpdate.UpdatedAt = eventDto.UpdatedAt; - eventToUpdate.Slug = eventDto.Slug; - eventToUpdate.StatusId = eventDto.StatusId; - - _context.Events.Update(eventToUpdate); - _context.SaveChanges(); - - var existingTags = _context.EventTags.Where(r => r.EventId == eventId).ToList(); - - var newTags = eventDto.Tags.Select(tagDto => new EventTag - { - Tag = new Tag - { - Name = tagDto.Name, - } - }).ToList(); - - foreach (var existingTag in existingTags) + return eventDto; + } + + public EventDto UpdateEvent(int eventId, EventDto eventDto) + { + using (var scope = new TransactionScope()) + { + var eventToUpdate = context.Events.FirstOrDefault(r => r.Id == eventId); + if (eventToUpdate == null) { - if (!newTags.Any(r => r.TagId == existingTag.TagId)) - { - _context.EventTags.Remove(existingTag); - } - } - - foreach (var newTag in newTags) + return null; + } + + eventToUpdate.Name = eventDto.Name; + eventToUpdate.AddressId = eventDto.AddressId; + eventToUpdate.Description = eventDto.Description; + eventToUpdate.OrganizerId = eventDto.OrganizerId; + eventToUpdate.StartAt = eventDto.StartAt; + eventToUpdate.EndAt = eventDto.EndAt; + eventToUpdate.UpdatedAt = DateTime.Now; + eventToUpdate.StatusId = eventDto.StatusId; + + context.Events.Update(eventToUpdate); + context.SaveChanges(); + + var existingTags = context.EventTags.Where(r => r.EventId == eventId).Include(eventTag => eventTag.Tag).ToList(); + + if (eventDto.Tags == null) { - if (!existingTags.Any(r => r.TagId == newTag.TagId)) + return eventDto; + } + + var newTags = eventDto.Tags + .Select(tagDto => new Tag { + Name = tagDto.Name, + }).ToList(); - _context.EventTags.Add(new EventTag { EventId = eventId, TagId = newTag.TagId }); - } - } - - return eventDto; - } - - public void DeleteEvent(int eventId) - { - var eventToDelete = _context.Events.FirstOrDefault(r => r.Id == eventId); + var tagsToRemove = from existingTag in existingTags + where newTags.All(r => r.Name != existingTag.Tag.Name) + select existingTag; + + foreach (var existingTag in tagsToRemove) + { + context.EventTags.Remove(existingTag); + } + + var tagsToAdd = from newTag in newTags + where existingTags.All(r => r.Tag.Name != newTag.Name) + select newTag; - if (eventToDelete != null) + foreach (var newTag in tagsToAdd) { - _context.EventTags.RemoveRange(_context.EventTags.Where(r => r.EventId == eventId)); - _context.Events.Remove(eventToDelete); + var existingTag = context.Tags.FirstOrDefault(t => t.Name == newTag.Name); + if (existingTag == null) + { + existingTag = new Tag { Name = newTag.Name }; + context.Tags.Add(existingTag); + context.SaveChanges(); + } + + context.EventTags.Add(new EventTag { EventId = eventId, TagId = existingTag.Id }); } - _context.SaveChanges(); - } + + context.SaveChanges(); + } + + return eventDto; + } - public EventDto GetEventById(int eventId) + public void DeleteEvent(int eventId) + { + using (var scope = new TransactionScope()) { - var eventToReturn = _context.Events.FirstOrDefault(r => r.Id == eventId); - if(eventToReturn == null) + var eventToDelete = context.Events.FirstOrDefault(r => r.Id == eventId); + + if (eventToDelete == null) { - return null; + return; } + + context.EventTags.RemoveRange(context.EventTags.Where(r => r.EventId == eventId)); + context.Events.Remove(eventToDelete); + + context.SaveChanges(); + } + } + + public EventDto GetEventById(int eventId) + { + var eventToReturn = context.Events.FirstOrDefault(r => r.Id == eventId); + if(eventToReturn == null) + { + return null; + } + + var eventTags = context.EventTags.Where(r => r.EventId == eventId).Include(e => e.Tag).ToList(); + var tags = new List(); + foreach (var eventTag in eventTags) + { + tags.Add(new TagDto { Name = eventTag.Tag.Name }); + } - var eventTags = _context.EventTags.Where(r => r.EventId == eventId).ToList(); + var eventDto = new EventDto + { + Name = eventToReturn.Name, + AddressId = eventToReturn.AddressId, + Description = eventToReturn.Description, + OrganizerId = eventToReturn.OrganizerId, + StartAt = eventToReturn.StartAt, + EndAt = eventToReturn.EndAt, + CreatedAt = eventToReturn.CreatedAt, + UpdatedAt = eventToReturn.UpdatedAt, + Slug = eventToReturn.Slug, + StatusId = eventToReturn.StatusId, + Tags = tags + }; + + return eventDto; + } + + public IEnumerable GetEventsByFilter(Expression> filter) + { + var events = context.Events.Where(filter).ToList(); + var eventDtos = new List(); + foreach (var eventToReturn in events) + { + var eventTags = context.EventTags.Where(r => r.EventId == eventToReturn.Id).Include(e => e.Tag).ToList().ToList(); var tags = new List(); foreach (var eventTag in eventTags) { - var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); - if (tag != null) tags.Add(new TagDto { Name = tag.Name }); + tags.Add(new TagDto { Name = eventTag.Tag.Name }); } var eventDto = new EventDto @@ -153,80 +202,50 @@ public EventDto GetEventById(int eventId) Tags = tags }; - return eventDto; + eventDtos.Add(eventDto); } - public IEnumerable GetEventsByFilter(Expression> filter) + return eventDtos; + } + + public IEnumerable GetAllEvents() + { + var events = context.Events.ToList(); + var eventDtos = new List(); + foreach (var eventToReturn in events) { - var events = _context.Events.Where(filter).ToList(); - var eventDtos = new List(); - foreach (var eventToReturn in events) + var eventTags = context.EventTags.Where(r => r.EventId == eventToReturn.Id).Include(e=> e.Tag).ToList(); + var tags = new List(); + foreach (var eventTag in eventTags) { - var eventTags = _context.EventTags.Where(r => r.EventId == eventToReturn.Id).ToList(); - var tags = new List(); - foreach (var eventTag in eventTags) - { - var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); - if (tag != null) tags.Add(new TagDto { Name = tag.Name }); - } - - var eventDto = new EventDto - { - Name = eventToReturn.Name, - AddressId = eventToReturn.AddressId, - Description = eventToReturn.Description, - OrganizerId = eventToReturn.OrganizerId, - StartAt = eventToReturn.StartAt, - EndAt = eventToReturn.EndAt, - CreatedAt = eventToReturn.CreatedAt, - UpdatedAt = eventToReturn.UpdatedAt, - Slug = eventToReturn.Slug, - StatusId = eventToReturn.StatusId, - Tags = tags - }; - - eventDtos.Add(eventDto); + tags.Add(new TagDto { Name = eventTag.Tag.Name }); } - return eventDtos; - } - - public IEnumerable GetAllEvents() - { - var events = _context.Events.ToList(); - var eventDtos = new List(); - foreach (var eventToReturn in events) + var eventDto = new EventDto { - var eventTags = _context.EventTags.Where(r => r.EventId == eventToReturn.Id).ToList(); - var tags = new List(); - foreach (var eventTag in eventTags) - { - var tag = _context.Tags.FirstOrDefault(r => r.Id == eventTag.TagId); - if (tag != null) tags.Add(new TagDto { Name = tag.Name }); - } + Name = eventToReturn.Name, + AddressId = eventToReturn.AddressId, + Description = eventToReturn.Description, + OrganizerId = eventToReturn.OrganizerId, + StartAt = eventToReturn.StartAt, + EndAt = eventToReturn.EndAt, + CreatedAt = eventToReturn.CreatedAt, + UpdatedAt = eventToReturn.UpdatedAt, + Slug = eventToReturn.Slug, + StatusId = eventToReturn.StatusId, + Tags = tags + }; - var eventDto = new EventDto - { - Name = eventToReturn.Name, - AddressId = eventToReturn.AddressId, - Description = eventToReturn.Description, - OrganizerId = eventToReturn.OrganizerId, - StartAt = eventToReturn.StartAt, - EndAt = eventToReturn.EndAt, - CreatedAt = eventToReturn.CreatedAt, - UpdatedAt = eventToReturn.UpdatedAt, - Slug = eventToReturn.Slug, - StatusId = eventToReturn.StatusId, - Tags = tags - }; + eventDtos.Add(eventDto); + } - eventDtos.Add(eventDto); - } + return eventDtos; + } - return eventDtos; - } - - - - } + private string CreateSlug(EventDto eventDto) + { + var slug = eventDto.Name.ToLower().Replace(" ", "-"); + return slug; + } + } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 098fb775..29d1b71b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -2,94 +2,93 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; -namespace ReasnAPI.Services { - public class ImageService (ReasnContext context) { - private readonly ReasnContext _context = context; +namespace ReasnAPI.Services; +public class ImageService (ReasnContext context) +{ - public ImageDto CreateImage(ImageDto imageDto) + public ImageDto CreateImage(ImageDto imageDto) + { + var newImage = new Image { - var newImage = new Image - { - ImageData = imageDto.ImageData, - ObjectId = imageDto.ObjectId, - ObjectTypeId = imageDto.ObjectTypeId - }; + ImageData = imageDto.ImageData, + ObjectId = imageDto.ObjectId, + ObjectTypeId = imageDto.ObjectTypeId + }; - _context.Images.Add(newImage); - _context.SaveChanges(); - return imageDto; - } + context.Images.Add(newImage); + context.SaveChanges(); + return imageDto; + } - public ImageDto UpdateImage(int imageId,ImageDto imageDto) + public ImageDto UpdateImage(int imageId,ImageDto imageDto) + { + var image = context.Images.FirstOrDefault(r => r.Id == imageId); + if (image == null) { - var image = _context.Images.FirstOrDefault(r => r.Id == imageId); - if (image == null) - { - return null; - } + return null; + } - image.ObjectId = imageDto.ObjectId; - image.ImageData = imageDto.ImageData; - image.ObjectTypeId = imageDto.ObjectTypeId; + image.ObjectId = imageDto.ObjectId; + image.ImageData = imageDto.ImageData; + image.ObjectTypeId = imageDto.ObjectTypeId; - _context.Images.Update(image); - _context.SaveChanges(); - return imageDto; - } + context.Images.Update(image); + context.SaveChanges(); + return imageDto; + } - public void DeleteImage(int id) - { - var image = _context.Images.Find(id); - if (image == null) { return;} - _context.Images.Remove(image); - _context.SaveChanges(); - - } + public void DeleteImage(int id) + { + var image = context.Images.Find(id); + if (image == null) { return;} + context.Images.Remove(image); + context.SaveChanges(); + + } - public ImageDto GetImageById(int id) + public ImageDto GetImageById(int id) + { + var image = context.Images.Find(id); + if (image == null) { - var image = _context.Images.Find(id); - if (image == null) - { - return null; - } + return null; + } - var imageDto = new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId - }; + var imageDto = new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }; - return imageDto; - } + return imageDto; + } - public IEnumerable GetAllImages() + public IEnumerable GetAllImages() + { + var images = context.Images.ToList(); + + var imageDtos = images.Select(image => new ImageDto { - var images = _context.Images.ToList(); - - var imageDtos = images.Select(image => new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId - }).ToList(); + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }).ToList(); - return imageDtos; - } + return imageDtos; + } - public IEnumerable GetImagesByFilter(Expression> filter) - { - var images = _context.Images.Where(filter).ToList(); + public IEnumerable GetImagesByFilter(Expression> filter) + { + var images = context.Images.Where(filter).ToList(); - var imageDtos = images.Select(image => new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId - }).ToList(); + var imageDtos = images.Select(image => new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }).ToList(); - return imageDtos; - } + return imageDtos; } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 5cd43ae1..cc839327 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -2,84 +2,82 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; -namespace ReasnAPI.Services { - public class InterestService (ReasnContext context) { - private readonly ReasnContext _context = context; +namespace ReasnAPI.Services; - public InterestDto CreateInterest(InterestDto interestDto) +public class InterestService (ReasnContext context) +{ + public InterestDto CreateInterest(InterestDto interestDto) + { + var newInterest = new Interest { - var newInterest = new Interest - { - Name = interestDto.Name - }; + Name = interestDto.Name + }; - _context.Interests.Add(newInterest); - _context.SaveChanges(); - return interestDto; - } + context.Interests.Add(newInterest); + context.SaveChanges(); + return interestDto; + } - public InterestDto UpdateInterest(int interestId,InterestDto interestDto) + public InterestDto UpdateInterest(int interestId,InterestDto interestDto) + { + var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); + if (interest == null) { - var interest = _context.Interests.FirstOrDefault(r => r.Id == interestId); - if (interest == null) - { - return null; - } + return null; + } - interest.Name = interestDto.Name; + interest.Name = interestDto.Name; - _context.Interests.Update(interest); - _context.SaveChanges(); - return interestDto; - } - - public void DeleteInterest(int id) - { - var interest = _context.Interests.Find(id); + context.Interests.Update(interest); + context.SaveChanges(); + return interestDto; + } + public void DeleteInterest(int id) + { + var interest = context.Interests.Find(id); - if (interest == null) - { - return; - } - _context.Interests.Remove(interest); - _context.SaveChanges(); - - } + if (interest == null) + { + return; + } + context.Interests.Remove(interest); + context.SaveChanges(); + + } - public InterestDto GetInterestById(int interestId) + public InterestDto GetInterestById(int interestId) + { + var interest = context.Interests.Find(interestId); + if (interest == null) { - var interest = _context.Interests.Find(interestId); - if (interest == null) - { - return null; - } + return null; + } - var interestDto = new InterestDto - { - Name = interest.Name - }; + var interestDto = new InterestDto + { + Name = interest.Name + }; - return interestDto; - } + return interestDto; + } - public IEnumerable GetAllInterests() - { - var interests = _context.Interests.ToList(); + public IEnumerable GetAllInterests() + { + var interests = context.Interests.ToList(); - return interests.Select(interest => new InterestDto { Name = interest.Name }).ToList(); - } + return interests.Select(interest => new InterestDto { Name = interest.Name }).ToList(); + } - public IEnumerable GetInterestsByFilter(Expression> filter) + public IEnumerable GetInterestsByFilter(Expression> filter) + { + var interests = context.Interests.Where(filter).ToList(); + + var interestDtos = interests.Select(interest => new InterestDto { - var interests = _context.Interests.Where(filter).ToList(); - - var interestDtos = interests.Select(interest => new InterestDto - { - Name = interest.Name - }).ToList(); + Name = interest.Name + }).ToList(); - return interestDtos; - } - + return interestDtos; } + } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 76c9a7e5..68e46036 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -2,79 +2,78 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; -namespace ReasnAPI.Services { - public class ParameterService (ReasnContext context){ - private readonly ReasnContext _context = context; +namespace ReasnAPI.Services; +public class ParameterService (ReasnContext context) +{ + public ParameterDto CreateParameter(ParameterDto parameterDto) + { + var parameter = new Parameter + { + Key = parameterDto.Key, + Value = parameterDto.Value + }; + context.Parameters.Add(parameter); + context.SaveChanges(); + return parameterDto; + } - public ParameterDto CreateParameter(ParameterDto parameterDto) + public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) + { + var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + + if(parameter == null) { - var parameter = new Parameter - { - Key = parameterDto.Key, - Value = parameterDto.Value - }; - _context.Parameters.Add(parameter); - _context.SaveChanges(); - return parameterDto; + return null; } + parameter.Key = parameterDto.Key; + parameter.Value = parameterDto.Value; + context.Parameters.Update(parameter); + context.SaveChanges(); + return parameterDto; + } - public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) + public void DeleteParameter(int parameterId) + { + var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + if (parameter == null) { - var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); - - if(parameter == null) - { - return null; - } - parameter.Key = parameterDto.Key; - parameter.Value = parameterDto.Value; - _context.Parameters.Update(parameter); - _context.SaveChanges(); - return parameterDto; + return; } + context.Parameters.Remove(parameter); + context.SaveChanges(); + } - public void DeleteParameter(int parameterId) + public ParameterDto GetParameterById(int parameterId) + { + var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + if(parameter == null) { - var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); - if (parameter == null) - { - return; - } - _context.Parameters.Remove(parameter); - _context.SaveChanges(); + return null; } - public ParameterDto GetParameterById(int parameterId) + var parameterDto = new ParameterDto { - var parameter = _context.Parameters.FirstOrDefault(r => r.Id == parameterId); - if(parameter == null) - { - return null; - } + Key = parameter.Key, + Value = parameter.Value + }; - var parameterDto = new ParameterDto - { - Key = parameter.Key, - Value = parameter.Value - }; - - return parameterDto; - } + return parameterDto; + } - public IEnumerable GetAllParameters() - { - var parameters = _context.Parameters.ToList(); + public IEnumerable GetAllParameters() + { + var parameters = context.Parameters.ToList(); - return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); - } + return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); + } - public IEnumerable GetParametersByFilter(Expression> filter) - { - var parameters = _context.Parameters.Where(filter).ToList(); + public IEnumerable GetParametersByFilter(Expression> filter) + { + var parameters = context.Parameters.Where(filter).ToList(); - return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); - - } + return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); } + } + diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 6995cd9b..43e99dc0 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -3,75 +3,71 @@ using System.Linq; using System.Linq.Expressions; -namespace ReasnAPI.Services { - public class TagService (ReasnContext context) { - private readonly ReasnContext _context = context; - - public TagDto CreateTag(TagDto tagDto) - { - var tag = _context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); - if (tag != null) return tagDto; - - var newTag = new Tag - { - Name = tagDto.Name - }; - _context.Tags.Add(newTag); - _context.SaveChanges(); - return tagDto; - } +namespace ReasnAPI.Services; +public class TagService (ReasnContext context) +{ + public TagDto CreateTag(TagDto tagDto) + { + var tag = context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); + if (tag != null) return tagDto; - public TagDto UpdateTag(int tagId,TagDto tagDto) + var newTag = new Tag { - var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); - if(tag == null) - { - return null; - } - tag.Name = tagDto.Name; - _context.Tags.Update(tag); - _context.SaveChanges(); - return tagDto; - } + Name = tagDto.Name + }; + context.Tags.Add(newTag); + context.SaveChanges(); + return tagDto; + } - public void DeleteTag(int tagId) + public TagDto UpdateTag(int tagId,TagDto tagDto) + { + var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); + if(tag == null) { - var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); - if (tag == null) - { - return; - } - _context.Tags.Remove(tag); - _context.SaveChanges(); + return null; } + tag.Name = tagDto.Name; + context.Tags.Update(tag); + context.SaveChanges(); + return tagDto; + } - public TagDto GetTagById(int tagId) + public void DeleteTag(int tagId) + { + var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); + if (tag == null) { - var tag = _context.Tags.FirstOrDefault(r => r.Id == tagId); - if(tag == null) - { - return null; - } - - return new TagDto - { - Name = tag.Name - }; + return; } + context.Tags.Remove(tag); + context.SaveChanges(); + } - public IEnumerable GetAllTags() + public TagDto GetTagById(int tagId) + { + var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); + if(tag == null) { - var tags = _context.Tags.ToList(); - - return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); + return null; } - public IEnumerable GetTagsByFilter(Expression> filter) + return new TagDto { - var tags = _context.Tags.Where(filter).ToList(); + Name = tag.Name + }; + } - return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); - } + public IEnumerable GetAllTags() + { + var tags = context.Tags.ToList(); + return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); + } + public IEnumerable GetTagsByFilter(Expression> filter) + { + var tags = context.Tags.Where(filter).ToList(); + return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); } + } From 6185066b88ac3a13d4e199916be69b6b6e7e92e2 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 17 Apr 2024 19:44:37 +0200 Subject: [PATCH 08/65] created test for interest status parameter and tag --- .../Services/ImageServiceTests.cs | 38 ++++ .../Services/InterestServiceTests.cs | 197 +++++++++++++++++ .../Services/ParameterServiceTests.cs | 200 ++++++++++++++++++ .../Services/StatusServiceTests.cs | 196 +++++++++++++++++ .../Services/TagServiceTests.cs | 185 +++++++++++++++- .../ReasnAPI/Models/DTOs/InterestDto.cs | 8 +- Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj | 1 - .../ReasnAPI/Services/ImageService.cs | 7 +- .../ReasnAPI/Services/InterestService.cs | 10 +- .../ReasnAPI/Services/ParameterService.cs | 10 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 5 +- 11 files changed, 843 insertions(+), 14 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs new file mode 100644 index 00000000..1f15c53b --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Linq.Expressions; +using Moq; +using Moq.EntityFrameworkCore; + + +namespace ReasnAPI.Tests.Services +{ + [TestClass] + public class ImageServiceTests + { + [TestMethod] + public void CreateImage_ImageDoesNotExist_ImageCreated() + { + var imageDto = new ImageDto + { + ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.CreateImage(imageDto); + Assert.IsNotNull(result); + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs new file mode 100644 index 00000000..02559b86 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -0,0 +1,197 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Linq.Expressions; +using Moq; +using Moq.EntityFrameworkCore; + +namespace ReasnAPI.Tests.Services +{ + [TestClass] + public class InterestServiceTests + { + [TestMethod] + public void CreateInterest_InterestDoesNotExist_InterestCreated() + { + var interestDto = new InterestDto + { + Name = "TestInterest" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.CreateInterest(interestDto); + + Assert.AreEqual(interestDto.Name, result.Name); + } + + [TestMethod] + public void CreateInterest_InterestExists_InterestNotCreated() + { + var interestDto = new InterestDto + { + Name = "TestInterest" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Name = "TestInterest" } }); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.CreateInterest(interestDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetAllInterests_InterestExists_InterestsReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Name = "TestInterest" } }); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.GetAllInterests(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void GetAllInterests_InterestDoesNotExists_NothingReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.GetAllInterests(); + + Assert.AreEqual(0, result.Count()); + } + + [TestMethod] + public void GetInterestById_InterestDoesNotExist_NullReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.GetInterestById(1); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetInterestById_InterestExists_InterestReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Id = 1, Name = "TestInterest"} }); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.GetInterestById(1); + + Assert.AreEqual("TestInterest", result.Name); + } + + [TestMethod] + public void UpdateInterest_InterestExists_InterestUpdated() + { + var interestDto = new InterestDto + { + Name = "TestInterest1" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Id = 1, Name = "TestInterest" } }); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.UpdateInterest(1, interestDto); + + Assert.AreEqual("TestInterest1", result.Name); + } + + [TestMethod] + public void UpdateInterest_InterestDoesNotExist_NullReturned() + { + var interestDto = new InterestDto + { + Name = "TestInterest" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.UpdateInterest(1, interestDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void DeleteInterest_InterestExists_InterestDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Id = 1, Name = "TestInterest" } }); + + var interestService = new InterestService(mockContext.Object); + + interestService.DeleteInterest(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Once); + } + + [TestMethod] + public void DeleteInterest_InterestDoesNotExist_NothingDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + + var interestService = new InterestService(mockContext.Object); + + interestService.DeleteInterest(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Never); + } + + [TestMethod] + public void GetInterestsByFilter_InterestExists_InterestReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Name = "TestInterest" } }); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.GetInterestsByFilter(i => i.Name == "TestInterest").ToList(); + + Assert.AreEqual(1, result.Count()); + + } + + [TestMethod] + public void GetInterestsByFilter_InterestDoesNotExist_NothingReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.GetInterestsByFilter(i => i.Name == "TestInterest").ToList(); + + Assert.AreEqual(0, result.Count()); + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs new file mode 100644 index 00000000..a66fe5be --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Linq.Expressions; +using Moq; +using Moq.EntityFrameworkCore; + +namespace ReasnAPI.Tests.Services +{ + [TestClass] + public class ParameterServiceTests + { + [TestMethod] + public void CreateParameter_ParameterDoesNotExist_ParameterCreated() + { + var parameterDto = new ParameterDto + { + Key = "TestKey", + Value = "TestValue" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.CreateParameter(parameterDto); + + Assert.AreEqual(parameterDto.Value, result.Value); + } + + [TestMethod] + public void CreateParameter_ParameterExists_ParameterNotCreated() + { + var parameterDto = new ParameterDto + { + Key = "TestKey", + Value = "TestValue" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Key = "TestKey", Value = "TestValue" } }); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.CreateParameter(parameterDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void UpdateParameter_ParameterExists_ParameterUpdated() + { + var parameterDto = new ParameterDto + { + Key = "TestKey", + Value = "TestValue" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.UpdateParameter(1, parameterDto); + + Assert.AreEqual(parameterDto.Value, result.Value); + } + + [TestMethod] + public void UpdateParameter_ParameterDoesNotExist_NullReturned() + { + var parameterDto = new ParameterDto + { + Key = "TestKey", + Value = "TestValue" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.UpdateParameter(1, parameterDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetParameterById_ParameterDoesNotExist_NullReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.GetParameterById(1); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetParameterById_ParameterExists_ParameterReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.GetParameterById(1); + + Assert.AreEqual("TestValue", result.Value); + } + + [TestMethod] + public void DeleteParameter_ParameterExists_ParameterDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + + var parameterService = new ParameterService(mockContext.Object); + + parameterService.DeleteParameter(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Once); + } + + [TestMethod] + public void DeleteParameter_ParameterDoesNotExist_NothingDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + + var parameterService = new ParameterService(mockContext.Object); + + parameterService.DeleteParameter(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Never); + } + + [TestMethod] + public void GetParameterByFilter_ParameterExists_ParameterReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.GetParametersByFilter(p => p.Key == "TestKey").ToList(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void GetParameterByFilter_ParameterDoesNotExist_NothingReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.GetParametersByFilter(p => p.Key == "TestKey").ToList(); + + Assert.AreEqual(0, result.Count()); + } + + [TestMethod] + public void GetAllParameters_ParameterExists_ParameterReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.GetAllParameters().ToList(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void GetAllParameters_ParameterDoesNotExist_NothingReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + + var parameterService = new ParameterService(mockContext.Object); + + var result = parameterService.GetAllParameters().ToList(); + + Assert.AreEqual(0, result.Count()); + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs new file mode 100644 index 00000000..72530098 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs @@ -0,0 +1,196 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Linq.Expressions; +using Moq; +using Moq.EntityFrameworkCore; + +namespace ReasnAPI.Tests.Services +{ + [TestClass] + public class StatusServiceTests + { + [TestMethod] + public void CreateStatus_StatusDoesNotExist_StatusCreated() + { + var statusDto = new StatusDto + { + Name = "TestStatus" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.CreateStatus(statusDto); + + Assert.AreEqual(statusDto.Name, result.Name); + } + + [TestMethod] + public void CreateStatus_StatusExists_StatusNotCreated() + { + var statusDto = new StatusDto + { + Name = "TestStatus" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Name = "TestStatus" } }); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.CreateStatus(statusDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void UpdateStatus_StatusExists_StatusUpdated() + { + var statusDto = new StatusDto + { + Name = "TestStatus" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status {Id = 1, Name = "TestStatus" } }); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.UpdateStatus(1, statusDto); + + Assert.AreEqual(statusDto.Name, result.Name); + } + + [TestMethod] + public void UpdateStatus_StatusDoesNotExist_StatusNotUpdated() + { + var statusDto = new StatusDto + { + Name = "TestStatus" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.UpdateStatus(1, statusDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetStatusById_StatusDoesNotExist_NullReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.GetStatusById(1); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetStatusById_StatusExists_StatusReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.GetStatusById(1); + + Assert.AreEqual("TestStatus", result.Name); + } + + [TestMethod] + public void DeleteStatus_StatusDoesNotExist_NothingDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); + + var statusService = new StatusService(mockContext.Object); + + statusService.DeleteStatus(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Never()); + } + + [TestMethod] + public void DeleteStatus_StatusExists_StatusDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); + + var statusService = new StatusService(mockContext.Object); + + statusService.DeleteStatus(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Once()); + } + + [TestMethod] + public void GetStatusByFilter_StatusExists_StatusReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.GetStatusesByFilter(s => s.Id == 1).ToList(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void GetStatusByFilter_StatusDoesNotExist_NothingReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.GetStatusesByFilter(s => s.Id == 1).ToList(); + + Assert.AreEqual(0, result.Count()); + } + + [TestMethod] + public void GetAllStatuses_StatusExists_StatusReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.GetAllStatuses().ToList(); + + Assert.AreEqual(1, result.Count()); + } + + [TestMethod] + public void GetAllStatuses_StatusDoesNotExist_NothingReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); + + var statusService = new StatusService(mockContext.Object); + + var result = statusService.GetAllStatuses().ToList(); + + Assert.AreEqual(0, result.Count()); + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 9bf6b13f..2d071241 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -1,17 +1,196 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using System.Threading.Tasks; using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using ReasnAPI.Services; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System.Linq.Expressions; +using Moq; +using Moq.EntityFrameworkCore; - namespace ReasnAPI.Tests.Services { - internal class TagServiceTests + [TestClass] + public class TagServiceTests { + [TestMethod] + public void CreateTag_TagDoesNotExist_TagCreated() + { + var tagDto = new TagDto + { + Name = "TestTag" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.CreateTag(tagDto); + + Assert.AreEqual(tagDto.Name, result.Name); + } + + [TestMethod] + public void CreateTag_TagExists_TagNotCreated() + { + var tagDto = new TagDto + { + Name = "TestTag" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.CreateTag(tagDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetAllTags_TagExists_TagsReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.GetAllTags().ToList(); + + Assert.AreEqual(1, result.Count); + } + + [TestMethod] + public void GetAllTags_TagNotExists_EmptyListReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.GetAllTags().ToList(); + + Assert.AreEqual(0, result.Count); + } + + [TestMethod] + public void GetTagById_TagExists_TagReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.GetTagById(1); + + Assert.AreEqual("TestTag", result.Name); + } + + [TestMethod] + public void GetTagById_TagDoesNotExist_NullReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.GetTagById(1); + + Assert.IsNull(result); + } + + [TestMethod] + public void UpdateTag_TagExists_TagUpdated() + { + var tagDto = new TagDto + { + Name = "TestTag1" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.UpdateTag(1, tagDto); + + Assert.AreEqual("TestTag1", result.Name); + } + + [TestMethod] + public void UpdateTag_TagDoesNotExist_NullReturned() + { + var tagDto = new TagDto + { + Name = "TestTag" + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.UpdateTag(1, tagDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void DeleteTag_TagExists_TagDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + tagService.DeleteTag(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Once); + } + + [TestMethod] + public void DeleteTag_TagDoesNotExist_NothingHappens() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + + var tagService = new TagService(mockContext.Object); + + tagService.DeleteTag(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Never); + } + + [TestMethod] + public void GetTagsByFilter_TagExist_TagsReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.GetTagsByFilter(t => t.Name == "TestTag").ToList(); + + Assert.AreEqual(1, result.Count); + } + + [TestMethod] + public void GetTagsByFilter_TagsNotExist_TagsNotReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + + var tagService = new TagService(mockContext.Object); + + var result = tagService.GetTagsByFilter(t => t.Name == "TestTag1").ToList(); + + Assert.AreEqual(0, result.Count); + } } } diff --git a/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs b/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs index 87eb141e..03872287 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs @@ -1,7 +1,7 @@ -namespace ReasnAPI.Models.DTOs -{ - public class InterestDto - { +using System; + +namespace ReasnAPI.Models.DTOs { + public class InterestDto { public string Name { get; set; } = null!; } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj index f3bbd683..d14bf77a 100644 --- a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj +++ b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj @@ -23,7 +23,6 @@ - diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 29d1b71b..ddfcecff 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -5,9 +5,14 @@ namespace ReasnAPI.Services; public class ImageService (ReasnContext context) { - public ImageDto CreateImage(ImageDto imageDto) { + var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectTypeId == imageDto.ObjectTypeId); + if (image != null) + { + return null; + } + var newImage = new Image { ImageData = imageDto.ImageData, diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index cc839327..99211113 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -8,6 +8,12 @@ public class InterestService (ReasnContext context) { public InterestDto CreateInterest(InterestDto interestDto) { + var interest = context.Interests.FirstOrDefault(r => r.Name == interestDto.Name); + if (interest != null) + { + return null; + } + var newInterest = new Interest { Name = interestDto.Name @@ -34,7 +40,7 @@ public InterestDto UpdateInterest(int interestId,InterestDto interestDto) } public void DeleteInterest(int id) { - var interest = context.Interests.Find(id); + var interest = context.Interests.FirstOrDefault(r => r.Id == id); if (interest == null) { @@ -47,7 +53,7 @@ public void DeleteInterest(int id) public InterestDto GetInterestById(int interestId) { - var interest = context.Interests.Find(interestId); + var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); if (interest == null) { return null; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 68e46036..ea888f2e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -7,12 +7,18 @@ public class ParameterService (ReasnContext context) { public ParameterDto CreateParameter(ParameterDto parameterDto) { - var parameter = new Parameter + var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key); + if (parameter != null) + { + return null; + } + + var newParameter = new Parameter { Key = parameterDto.Key, Value = parameterDto.Value }; - context.Parameters.Add(parameter); + context.Parameters.Add(newParameter); context.SaveChanges(); return parameterDto; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 43e99dc0..bc62f8c1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -9,7 +9,10 @@ public class TagService (ReasnContext context) public TagDto CreateTag(TagDto tagDto) { var tag = context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); - if (tag != null) return tagDto; + if (tag != null) + { + return null; + } var newTag = new Tag { From db6a70f350035b7f69231f3a0fdb005a742a3433 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 18 Apr 2024 13:02:46 +0200 Subject: [PATCH 09/65] Created ImageServicesTests --- .../Services/EventServicesTest.cs | 19 ++ .../Services/ImageServiceTests.cs | 172 ++++++++++++++++++ Server/ReasnAPI/ReasnAPI/Program.cs | 27 ++- Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj | 1 - .../ReasnAPI/Services/ImageService.cs | 8 +- 5 files changed, 209 insertions(+), 18 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs new file mode 100644 index 00000000..49f703b6 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; +using ReasnAPI.Models.Database; + + +namespace ReasnAPI.Tests.Services +{ + [TestClass] + internal class EventServicesTest + { + + + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 1f15c53b..02e3ce66 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -34,5 +34,177 @@ public void CreateImage_ImageDoesNotExist_ImageCreated() Assert.IsNotNull(result); } + [TestMethod] + public void CreateImage_ImageExists_ImageNotCreated() + { + var imageDto = new ImageDto + { + ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.CreateImage(imageDto); + Assert.IsNull(result); + } + + [TestMethod] + public void GetAllImages_ImageExists_ImagesReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.GetAllImages(); + + Assert.IsNotNull(result); + } + + [TestMethod] + public void GetAllImages_ImageNotExists_EmptyListReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.GetAllImages().ToList(); + + Assert.AreEqual(0,result.Count); + } + + [TestMethod] + public void GetImageById_ImageExists_ImageReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { Id = 1, ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.GetImageById(1); + Assert.IsNotNull(result); + } + + [TestMethod] + public void GetImageById_ImageDoesNotExist_NullReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.GetImageById(1); + Assert.IsNull(result); + } + + [TestMethod] + public void UpdateImage_ImageExists_ImageUpdated() + { + var imageDto = new ImageDto + { + ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { Id = 1, ImageData = new byte[] { } } }); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.UpdateImage(1, imageDto); + + CollectionAssert.AreEqual(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, result.ImageData); + } + + [TestMethod] + + public void UpdateImage_ImageDoesNotExist_NullReturned() + { + var imageDto = new ImageDto + { + ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } + }; + + var mockContext = new Mock(); + + mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.UpdateImage(1, imageDto); + + Assert.IsNull(result); + } + + [TestMethod] + + public void DeleteImage_ImageExists_ImageDeleted() + { + var mockContext = new Mock(); + + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { Id = 1, ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); + + var imageService = new ImageService(mockContext.Object); + + imageService.DeleteImage(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Once); + } + + [TestMethod] + + public void DeleteImage_ImageDoesNotExist_NothingDeleted() + { + var mockContext = new Mock(); + + mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); + + var imageService = new ImageService(mockContext.Object); + + imageService.DeleteImage(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Never); + } + + [TestMethod] + + public void GetImageByFilter_ImageExists_ImageReturned() + { + var mockContext = new Mock(); + + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { Id = 1, ImageData = new byte[] { } } }); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.GetImagesByFilter(i => i.Id == 1); + + Assert.IsNotNull(result); + } + + [TestMethod] + + public void GetImageByFilter_ImageDoesNotExist_NothingReturned() + { + var mockContext = new Mock(); + + mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); + + var imageService = new ImageService(mockContext.Object); + + var result = imageService.GetImagesByFilter(i => i.Id == 1); + + Assert.AreEqual(0, result.Count()); + } + + } } diff --git a/Server/ReasnAPI/ReasnAPI/Program.cs b/Server/ReasnAPI/ReasnAPI/Program.cs index 8b2051c9..b7382108 100644 --- a/Server/ReasnAPI/ReasnAPI/Program.cs +++ b/Server/ReasnAPI/ReasnAPI/Program.cs @@ -1,27 +1,26 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; -using Npgsql; -using ReasnAPI.Models.Enums; using Serilog; using System; using System.Text.Json.Serialization; +using ReasnAPI.Models.DTOs; using ReasnAPI.Models.Database; using ReasnAPI.Services; var builder = WebApplication.CreateSlimBuilder(args); -var connectionString = builder.Configuration.GetConnectionString("DefaultValue"); - -var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString); -dataSourceBuilder.MapEnum("common.object_type"); -dataSourceBuilder.MapEnum("common.event_status"); -dataSourceBuilder.MapEnum("common.participant_status"); -dataSourceBuilder.MapEnum("users.role"); - -var dataSource = dataSourceBuilder.Build(); - -builder.Services.AddDbContext(options => +var connectionString = builder.Configuration.GetConnectionString("DefaultValue"); + +var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString); +dataSourceBuilder.MapEnum("common.object_type"); +dataSourceBuilder.MapEnum("common.event_status"); +dataSourceBuilder.MapEnum("common.participant_status"); +dataSourceBuilder.MapEnum("users.role"); + +var dataSource = dataSourceBuilder.Build(); + +builder.Services.AddDbContext(options => options.UseNpgsql(dataSource)); builder.Services.AddControllers(); @@ -61,4 +60,4 @@ app.MapGet("/", () => "Hello, World!"); -app.Run(); \ No newline at end of file +app.Run(); diff --git a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj index d14bf77a..25ee8a40 100644 --- a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj +++ b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj @@ -21,7 +21,6 @@ - diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index ddfcecff..1eacf290 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -1,7 +1,9 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; - +using static System.Net.Mime.MediaTypeNames; +using Image = ReasnAPI.Models.Database.Image; + namespace ReasnAPI.Services; public class ImageService (ReasnContext context) { @@ -44,7 +46,7 @@ public ImageDto UpdateImage(int imageId,ImageDto imageDto) public void DeleteImage(int id) { - var image = context.Images.Find(id); + var image = context.Images.FirstOrDefault(r => r.Id == id); if (image == null) { return;} context.Images.Remove(image); context.SaveChanges(); @@ -53,7 +55,7 @@ public void DeleteImage(int id) public ImageDto GetImageById(int id) { - var image = context.Images.Find(id); + var image = context.Images.FirstOrDefault(r => r.Id == id); if (image == null) { return null; From ce92d22cbe602a58711ed905329f75c4cc033be8 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 1 May 2024 13:44:47 +0200 Subject: [PATCH 10/65] Updated Parameter, Tag, Status services Checked if any of Parameters, Tags or Statuses are used before delating/updating. --- .../Services/EventServicesTest.cs | 479 +++++++++++++++++- .../Services/ImageServiceTests.cs | 4 +- .../ReasnAPI/Controllers/TestController.cs | 34 ++ .../ReasnAPI/Services/EventService.cs | 105 ++-- .../ReasnAPI/Services/ImageService.cs | 83 ++- .../ReasnAPI/Services/InterestService.cs | 12 +- .../ReasnAPI/Services/ParameterService.cs | 36 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 16 +- 8 files changed, 686 insertions(+), 83 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 49f703b6..24a77b23 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -6,14 +6,491 @@ using ReasnAPI.Models.DTOs; using ReasnAPI.Services; using ReasnAPI.Models.Database; +using Moq; +using Moq.EntityFrameworkCore; namespace ReasnAPI.Tests.Services { [TestClass] - internal class EventServicesTest + public class EventServicesTest { + [TestMethod] + public void CreateEvent_EventDoesNotExist_EventCreated() + { + var tagDto = new TagDto() + { + Name = "tesTag" + }; + var tagList = new List + { + tagDto + }; + var eventDto = new EventDto() + { + Name = "name", + AddressId = 1, + Description = "description", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + StatusId = 1, + Tags = tagList, + }; + var mockContext = new Mock(); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Roles).ReturnsDbSet(new List + { + new Role() + { + Id = 1, + Name = "test" + } + }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + }}); + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, + RoleId = 1, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now + }}); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List + { + new Status() + { + Id = 1, + Name = "TestStatus" + } + }); + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + + var eventService = new EventService(mockContext.Object); + + var result = eventService.CreateEvent(eventDto); + Assert.IsNotNull(result); + Assert.AreEqual(1,result.Tags.Count); + + } + + [TestMethod] + public void UpdateEvent_EventExists_EventUpdated() + { + var tagDto = new TagDto() { Name = "tesTag" }; + var tagList = new List + { + tagDto + }; + var eventDto = new EventDto() + { + Name = "name1", + AddressId = 1, + Description = "description2", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + StatusId = 1, + Tags = tagList, + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List{ + new Event() + { + Id = 1, + Name = "name", + AddressId = 1, + Description = "description", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + StatusId = 1, + }}); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Roles).ReturnsDbSet(new List + { + new Role() + { + Id = 1, + Name = "test" + } + }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + }}); + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, RoleId = 1, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now }}); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { + new Status() + { + Id = 1, + Name = "TestStatus" + } }); + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + var eventService = new EventService(mockContext.Object); + + var result = eventService.UpdateEvent(1,eventDto); + Assert.AreEqual("name1",result.Name); + Assert.AreEqual("description2",result.Description); + Assert.AreEqual(1, result.Tags.Count); + + } + + [TestMethod] + public void UpdateEvent_EventDoesNotExist_NullReturned() + { + var tagDto = new TagDto() { Name = "tesTag" }; + var tagList = new List + { + tagDto + }; + var eventDto = new EventDto() + { + Name = "name1", + AddressId = 1, + Description = "description2", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + StatusId = 1, + Tags = tagList, + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Roles).ReturnsDbSet(new List + { + new Role() + { + Id = 1, + Name = "test" + } + }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + }}); + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, + RoleId = 1, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now + }}); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List + { + new Status() + { + Id = 1, + Name = "TestStatus" + } + }); + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + var eventService = new EventService(mockContext.Object); + + var result = eventService.UpdateEvent(1, eventDto); + + Assert.IsNull(result); + } + + [TestMethod] + public void GetEventById_EventExists_EventReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List + { + new Event() + { + Id = 1, + Name = "name", + Slug = "name", + AddressId = 1, + Description = "description", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + StatusId = 1, + }}); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List + { + new Tag() + { + Id = 1, + Name = "name" + } + }); + mockContext.Setup(c => c.Roles).ReturnsDbSet(new List + { + new Role() + { + Id = 1, + Name = "test" + } + }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + }}); + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, + RoleId = 1, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now }}); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List + { + new Status() + { + Id = 1, + Name = "TestStatus" + } + }); + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List + { + new EventTag() + { + EventId = 1, + TagId = 1 + } + + }); + + var eventService = new EventService(mockContext.Object); + + var result = eventService.GetEventById(1); + Assert.IsNotNull(result); + + } + + [TestMethod] + + public void GetEventById_EventDoesNotExist_NullReturned() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Roles).ReturnsDbSet(new List + { + new Role() + { + Id = 1, + Name = "test" + } + }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
+ { + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + }}); + mockContext.Setup(c => c.Users).ReturnsDbSet(new List + { + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, + RoleId = 1, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now + }}); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List + { + new Status() + { + Id = 1, + Name = "TestStatus" + } + }); + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + + var eventService = new EventService(mockContext.Object); + + var result = eventService.GetEventById(1); + + Assert.IsNull(result); + } + + [TestMethod] + + public void DeleteEvent_EventExists_EventDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List + { + new Event() + { + Id = 1, + Name = "name", + Slug = "name", + AddressId = 1, + Description = "description", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + StatusId = 1, + } + }); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List + { + new Tag() + { + Id = 1, + Name = "name" + } + }); + mockContext.Setup(c => c.Roles).ReturnsDbSet(new List + { + new Role() + { + Id = 1, + Name = "test" + } + }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
+ { + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + } + }); + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, + RoleId = 1, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now }}); + mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List + { + new Status() + { + Id = 1, + Name = "TestStatus" + } + }); + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List + { + new EventTag() + { + EventId = 1, + TagId = 1 + } + + }); + + var eventService = new EventService(mockContext.Object); + + eventService.DeleteEvent(1); + + mockContext.Verify(c => c.SaveChanges(), Times.Once); + } } } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 02e3ce66..4eede59f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -185,7 +185,7 @@ public void GetImageByFilter_ImageExists_ImageReturned() var imageService = new ImageService(mockContext.Object); - var result = imageService.GetImagesByFilter(i => i.Id == 1); + var result = imageService.GetImagesByFilter(i => i.Id == 1).ToList(); Assert.IsNotNull(result); } @@ -200,7 +200,7 @@ public void GetImageByFilter_ImageDoesNotExist_NothingReturned() var imageService = new ImageService(mockContext.Object); - var result = imageService.GetImagesByFilter(i => i.Id == 1); + var result = imageService.GetImagesByFilter(i => i.Id == 1).ToList(); Assert.AreEqual(0, result.Count()); } diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs new file mode 100644 index 00000000..a31e551d --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services; + +namespace ReasnAPI.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestController : Controller + { + + private readonly TagService _tagService; + + public TestController(TagService tagService) + { + _tagService = tagService; + } + + [HttpGet] + public IActionResult Add() + { + + var tagDto = new TagDto + { + Name = "TestTagasd" + }; + + var result = _tagService.CreateTag(tagDto); + + return Ok(result); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 399c6f86..a527a224 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -6,7 +6,7 @@ using System.Transactions; namespace ReasnAPI.Services; -public class EventService (ReasnContext context) +public class EventService(ReasnContext context) { public EventDto CreateEvent(EventDto eventDto) { @@ -27,7 +27,7 @@ public EventDto CreateEvent(EventDto eventDto) Slug = eventDto.Slug, StatusId = eventDto.StatusId, }; - + context.Events.Add(newEvent); context.SaveChanges(); @@ -39,17 +39,31 @@ public EventDto CreateEvent(EventDto eventDto) foreach (var tag in eventDto.Tags) { - var newTag = new Tag { Name = tag.Name }; - context.Tags.Add(newTag); - context.SaveChanges(); + var newTag = context.Tags.FirstOrDefault(r => r.Name == tag.Name); + if (newTag == null) + { + var tagToAdd = new Tag { Name = tag.Name }; + context.Tags.Add(tagToAdd); + context.SaveChanges(); - var eventTag = new EventTag + var eventTag = new EventTag + { + EventId = eventId, + TagId = tagToAdd.Id + }; + context.EventTags.Add(eventTag); + context.SaveChanges(); + } + else { - EventId = eventId, - TagId = newTag.Id - }; - context.EventTags.Add(eventTag); - context.SaveChanges(); + var eventTag = new EventTag + { + EventId = eventId, + TagId = newTag.Id + }; + context.EventTags.Add(eventTag); + context.SaveChanges(); + } } } @@ -84,7 +98,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) { return eventDto; } - + var newTags = eventDto.Tags .Select(tagDto => new Tag { @@ -92,8 +106,8 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) }).ToList(); var tagsToRemove = from existingTag in existingTags - where newTags.All(r => r.Name != existingTag.Tag.Name) - select existingTag; + where newTags.All(r => r.Name != existingTag.Tag.Name) + select existingTag; foreach (var existingTag in tagsToRemove) { @@ -101,8 +115,8 @@ where newTags.All(r => r.Name != existingTag.Tag.Name) } var tagsToAdd = from newTag in newTags - where existingTags.All(r => r.Tag.Name != newTag.Name) - select newTag; + where existingTags.All(r => r.Tag.Name != newTag.Name) + select newTag; foreach (var newTag in tagsToAdd) { @@ -113,10 +127,10 @@ where existingTags.All(r => r.Tag.Name != newTag.Name) context.Tags.Add(existingTag); context.SaveChanges(); } - + context.EventTags.Add(new EventTag { EventId = eventId, TagId = existingTag.Id }); } - + context.SaveChanges(); } @@ -133,10 +147,10 @@ public void DeleteEvent(int eventId) { return; } - + context.EventTags.RemoveRange(context.EventTags.Where(r => r.EventId == eventId)); context.Events.Remove(eventToDelete); - + context.SaveChanges(); } } @@ -144,17 +158,20 @@ public void DeleteEvent(int eventId) public EventDto GetEventById(int eventId) { var eventToReturn = context.Events.FirstOrDefault(r => r.Id == eventId); - if(eventToReturn == null) + if (eventToReturn == null) { return null; } - var eventTags = context.EventTags.Where(r => r.EventId == eventId).Include(e => e.Tag).ToList(); - var tags = new List(); - foreach (var eventTag in eventTags) - { - tags.Add(new TagDto { Name = eventTag.Tag.Name }); - } + var tagIds = context.EventTags + .Where(r => r.EventId == eventId) + .Select(r => r.TagId) + .ToList(); + + var tags = context.Tags + .Where(t => tagIds.Contains(t.Id)) + .Select(t => new TagDto { Name = t.Name }) + .ToList(); var eventDto = new EventDto { @@ -180,12 +197,15 @@ public IEnumerable GetEventsByFilter(Expression> fil var eventDtos = new List(); foreach (var eventToReturn in events) { - var eventTags = context.EventTags.Where(r => r.EventId == eventToReturn.Id).Include(e => e.Tag).ToList().ToList(); - var tags = new List(); - foreach (var eventTag in eventTags) - { - tags.Add(new TagDto { Name = eventTag.Tag.Name }); - } + var tagIds = context.EventTags + .Where(r => r.EventId == eventToReturn.Id) + .Select(r => r.TagId) + .ToList(); + + var tags = context.Tags + .Where(t => tagIds.Contains(t.Id)) + .Select(t => new TagDto { Name = t.Name }) + .ToList(); var eventDto = new EventDto { @@ -214,12 +234,15 @@ public IEnumerable GetAllEvents() var eventDtos = new List(); foreach (var eventToReturn in events) { - var eventTags = context.EventTags.Where(r => r.EventId == eventToReturn.Id).Include(e=> e.Tag).ToList(); - var tags = new List(); - foreach (var eventTag in eventTags) - { - tags.Add(new TagDto { Name = eventTag.Tag.Name }); - } + var tagIds = context.EventTags + .Where(r => r.EventId == eventToReturn.Id) + .Select(r => r.TagId) + .ToList(); + + var tags = context.Tags + .Where(t => tagIds.Contains(t.Id)) + .Select(t => new TagDto { Name = t.Name }) + .ToList(); var eventDto = new EventDto { @@ -247,5 +270,5 @@ private string CreateSlug(EventDto eventDto) var slug = eventDto.Name.ToLower().Replace(" ", "-"); return slug; } - -} + +} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 1eacf290..56e1447f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -5,29 +5,52 @@ using Image = ReasnAPI.Models.Database.Image; namespace ReasnAPI.Services; -public class ImageService (ReasnContext context) -{ - public ImageDto CreateImage(ImageDto imageDto) +public class ImageService(ReasnContext context) +{ + public List CreateImages(List imageDtos) { - var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectTypeId == imageDto.ObjectTypeId); - if (image != null) + var newImages = new List(); + + foreach (var imageDto in imageDtos) { - return null; + var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectTypeId == imageDto.ObjectTypeId); + if (image != null) + { + continue; + } + + var newImage = new Image + { + ImageData = imageDto.ImageData, + ObjectId = imageDto.ObjectId, + ObjectTypeId = imageDto.ObjectTypeId + }; + + newImages.Add(newImage); } - var newImage = new Image + if (newImages.Any()) { - ImageData = imageDto.ImageData, - ObjectId = imageDto.ObjectId, - ObjectTypeId = imageDto.ObjectTypeId - }; + var objectType = context.ObjectTypes.FirstOrDefault(ot => ot.Id == newImages.First().ObjectTypeId); + if (objectType != null) + { + if (objectType.Name == "User" && newImages.Count > 1) + { + context.Images.Add(newImages.First()); + } + else if (objectType.Name == "Event") + { + context.Images.AddRange(newImages); + } + } - context.Images.Add(newImage); - context.SaveChanges(); - return imageDto; + context.SaveChanges(); + } + + return imageDtos; } - public ImageDto UpdateImage(int imageId,ImageDto imageDto) + public ImageDto UpdateImage(int imageId, ImageDto imageDto) { var image = context.Images.FirstOrDefault(r => r.Id == imageId); if (image == null) @@ -47,10 +70,13 @@ public ImageDto UpdateImage(int imageId,ImageDto imageDto) public void DeleteImage(int id) { var image = context.Images.FirstOrDefault(r => r.Id == id); - if (image == null) { return;} + if (image == null) + { + return; + } context.Images.Remove(image); context.SaveChanges(); - + } public ImageDto GetImageById(int id) @@ -74,7 +100,7 @@ public ImageDto GetImageById(int id) public IEnumerable GetAllImages() { var images = context.Images.ToList(); - + var imageDtos = images.Select(image => new ImageDto { ImageData = image.ImageData, @@ -97,5 +123,22 @@ public IEnumerable GetImagesByFilter(Expression> fil }).ToList(); return imageDtos; - } -} + } + + public IEnumerable GetImagesByEventIdAndType(int eventId) + { + var images = context.Images + .Where(image => image.ObjectType.Name == "event" && image.ObjectId == eventId) + .ToList(); + + var imageDtos = images.Select(image => new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectTypeId = image.ObjectTypeId + }).ToList(); + + return imageDtos; + } + +} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 99211113..94b4388e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -4,7 +4,7 @@ namespace ReasnAPI.Services; -public class InterestService (ReasnContext context) +public class InterestService(ReasnContext context) { public InterestDto CreateInterest(InterestDto interestDto) { @@ -24,7 +24,7 @@ public InterestDto CreateInterest(InterestDto interestDto) return interestDto; } - public InterestDto UpdateInterest(int interestId,InterestDto interestDto) + public InterestDto UpdateInterest(int interestId, InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); if (interest == null) @@ -45,10 +45,10 @@ public void DeleteInterest(int id) if (interest == null) { return; - } + } context.Interests.Remove(interest); context.SaveChanges(); - + } public InterestDto GetInterestById(int interestId) @@ -77,7 +77,7 @@ public IEnumerable GetAllInterests() public IEnumerable GetInterestsByFilter(Expression> filter) { var interests = context.Interests.Where(filter).ToList(); - + var interestDtos = interests.Select(interest => new InterestDto { Name = interest.Name @@ -86,4 +86,4 @@ public IEnumerable GetInterestsByFilter(Expression r.Key == parameterDto.Key); - if (parameter != null) + if (parameter != null && parameter.Value == parameterDto.Value) { return null; } @@ -23,11 +23,17 @@ public ParameterDto CreateParameter(ParameterDto parameterDto) return parameterDto; } - public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) + public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); - - if(parameter == null) + + var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); + if (parameterCheck != null) // if parameter is associated with an event, it cannot be updated + { + return null; + } + + if (parameter == null) { return null; } @@ -41,6 +47,13 @@ public ParameterDto UpdateParameter(int parameterId,ParameterDto parameterDto) public void DeleteParameter(int parameterId) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + + var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); + if (parameterCheck != null) // if parameter is associated with an event, it cannot be deleted + { + return; + } + if (parameter == null) { return; @@ -52,7 +65,7 @@ public void DeleteParameter(int parameterId) public ParameterDto GetParameterById(int parameterId) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); - if(parameter == null) + if (parameter == null) { return null; } @@ -73,13 +86,12 @@ public IEnumerable GetAllParameters() return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); } - public IEnumerable GetParametersByFilter(Expression> filter) - { + public IEnumerable GetParametersByFilter(Expression> filter) + { var parameters = context.Parameters.Where(filter).ToList(); - return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); - + return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }) + .ToList(); } - -} +} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index bc62f8c1..f9f208dc 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -26,6 +26,13 @@ public TagDto CreateTag(TagDto tagDto) public TagDto UpdateTag(int tagId,TagDto tagDto) { 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 + { + return null; + } + if(tag == null) { return null; @@ -39,6 +46,13 @@ public TagDto UpdateTag(int tagId,TagDto tagDto) public void DeleteTag(int tagId) { 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 deleted + { + return; + } + if (tag == null) { return; @@ -73,4 +87,4 @@ public IEnumerable GetTagsByFilter(Expression> filter) return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); } -} +} \ No newline at end of file From 63f3d7726d3c47fab7fe0c07112b4e27a555acb7 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 2 May 2024 15:05:42 +0200 Subject: [PATCH 11/65] fixed bug in imageservicetest --- .../ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 4eede59f..6e64fb45 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -17,6 +17,7 @@ namespace ReasnAPI.Tests.Services [TestClass] public class ImageServiceTests { + [TestMethod] public void CreateImage_ImageDoesNotExist_ImageCreated() { @@ -29,8 +30,9 @@ public void CreateImage_ImageDoesNotExist_ImageCreated() mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); var imageService = new ImageService(mockContext.Object); + List imagedtoslist = [imageDto]; - var result = imageService.CreateImage(imageDto); + var result = imageService.CreateImages(imagedtoslist); Assert.IsNotNull(result); } @@ -47,8 +49,8 @@ public void CreateImage_ImageExists_ImageNotCreated() { new Image { ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); var imageService = new ImageService(mockContext.Object); - - var result = imageService.CreateImage(imageDto); + List imagedtoslist = [imageDto]; + var result = imageService.CreateImages(imagedtoslist); Assert.IsNull(result); } From e760dbbefc851ecbfab873e797cdee558c06debf Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 2 May 2024 15:18:23 +0200 Subject: [PATCH 12/65] one more fix --- .../Services/ImageServiceTests.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 6e64fb45..5745330c 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -30,7 +30,8 @@ public void CreateImage_ImageDoesNotExist_ImageCreated() mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); var imageService = new ImageService(mockContext.Object); - List imagedtoslist = [imageDto]; + List imagedtoslist = new List(); + imagedtoslist.Append(imageDto); var result = imageService.CreateImages(imagedtoslist); Assert.IsNotNull(result); @@ -49,9 +50,13 @@ public void CreateImage_ImageExists_ImageNotCreated() { new Image { ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); var imageService = new ImageService(mockContext.Object); - List imagedtoslist = [imageDto]; + List imagedtoslist = new List(); + imagedtoslist.Append(imageDto); + var result = imageService.CreateImages(imagedtoslist); - Assert.IsNull(result); + + mockContext.Verify(m => m.Add(It.IsAny()), Times.Never()); + mockContext.Verify(m => m.AddRange(It.IsAny>()), Times.Never()); } [TestMethod] @@ -64,7 +69,7 @@ public void GetAllImages_ImageExists_ImagesReturned() var imageService = new ImageService(mockContext.Object); var result = imageService.GetAllImages(); - + Assert.IsNotNull(result); } @@ -77,8 +82,8 @@ public void GetAllImages_ImageNotExists_EmptyListReturned() var imageService = new ImageService(mockContext.Object); var result = imageService.GetAllImages().ToList(); - - Assert.AreEqual(0,result.Count); + + Assert.AreEqual(0, result.Count); } [TestMethod] From 196ef6155eed8ea7f09d2015f4d284d226c92c77 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 6 May 2024 12:42:10 +0200 Subject: [PATCH 13/65] Updated services and tests Changed delete methods to bool, updated mocks on tests. --- .../ReasnAPI.Tests/Services/ParameterServiceTests.cs | 7 ++++--- .../ReasnAPI.Tests/Services/StatusServiceTests.cs | 6 ++++-- .../ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs | 8 ++++---- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 6 ++++-- Server/ReasnAPI/ReasnAPI/Services/ImageService.cs | 5 +++-- Server/ReasnAPI/ReasnAPI/Services/InterestService.cs | 5 +++-- Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs | 7 ++++--- Server/ReasnAPI/ReasnAPI/Services/TagService.cs | 8 +++++--- 8 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index a66fe5be..2884b1df 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -65,6 +65,7 @@ public void UpdateParameter_ParameterExists_ParameterUpdated() var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); @@ -84,7 +85,7 @@ public void UpdateParameter_ParameterDoesNotExist_NullReturned() var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); var result = parameterService.UpdateParameter(1, parameterDto); @@ -123,7 +124,7 @@ public void DeleteParameter_ParameterExists_ParameterDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); - + mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); parameterService.DeleteParameter(1); @@ -136,7 +137,7 @@ public void DeleteParameter_ParameterDoesNotExist_NothingDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); parameterService.DeleteParameter(1); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs index 72530098..7e59c56f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs @@ -119,7 +119,8 @@ public void DeleteStatus_StatusDoesNotExist_NothingDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); var statusService = new StatusService(mockContext.Object); statusService.DeleteStatus(1); @@ -132,7 +133,8 @@ public void DeleteStatus_StatusExists_StatusDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); - + mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); var statusService = new StatusService(mockContext.Object); statusService.DeleteStatus(1); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 2d071241..987dd6d1 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -114,7 +114,7 @@ public void UpdateTag_TagExists_TagUpdated() var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); - + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); var result = tagService.UpdateTag(1, tagDto); @@ -132,7 +132,7 @@ public void UpdateTag_TagDoesNotExist_NullReturned() var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); var result = tagService.UpdateTag(1, tagDto); @@ -145,7 +145,7 @@ public void DeleteTag_TagExists_TagDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); - + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); tagService.DeleteTag(1); @@ -158,7 +158,7 @@ public void DeleteTag_TagDoesNotExist_NothingHappens() { var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); tagService.DeleteTag(1); diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index a527a224..dd22cb1d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -137,7 +137,7 @@ where existingTags.All(r => r.Tag.Name != newTag.Name) return eventDto; } - public void DeleteEvent(int eventId) + public bool DeleteEvent(int eventId) { using (var scope = new TransactionScope()) { @@ -145,7 +145,7 @@ public void DeleteEvent(int eventId) if (eventToDelete == null) { - return; + return false; } context.EventTags.RemoveRange(context.EventTags.Where(r => r.EventId == eventId)); @@ -153,6 +153,8 @@ public void DeleteEvent(int eventId) context.SaveChanges(); } + + return true; } public EventDto GetEventById(int eventId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 56e1447f..6feeeef4 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -67,16 +67,17 @@ public ImageDto UpdateImage(int imageId, ImageDto imageDto) return imageDto; } - public void DeleteImage(int id) + public bool DeleteImage(int id) { var image = context.Images.FirstOrDefault(r => r.Id == id); if (image == null) { - return; + return false; } context.Images.Remove(image); context.SaveChanges(); + return true; } public ImageDto GetImageById(int id) diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 94b4388e..18277240 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -38,17 +38,18 @@ public InterestDto UpdateInterest(int interestId, InterestDto interestDto) context.SaveChanges(); return interestDto; } - public void DeleteInterest(int id) + public bool DeleteInterest(int id) { var interest = context.Interests.FirstOrDefault(r => r.Id == id); if (interest == null) { - return; + return false; } context.Interests.Remove(interest); context.SaveChanges(); + return true; } public InterestDto GetInterestById(int interestId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 429baf47..5923229f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -44,22 +44,23 @@ public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) return parameterDto; } - public void DeleteParameter(int parameterId) + public bool DeleteParameter(int parameterId) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); if (parameterCheck != null) // if parameter is associated with an event, it cannot be deleted { - return; + return false; } if (parameter == null) { - return; + return false; } context.Parameters.Remove(parameter); context.SaveChanges(); + return true; } public ParameterDto GetParameterById(int parameterId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index f9f208dc..986c6904 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -43,22 +43,24 @@ public TagDto UpdateTag(int tagId,TagDto tagDto) return tagDto; } - public void DeleteTag(int tagId) + public bool DeleteTag(int tagId) { 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 deleted { - return; + return false; } if (tag == null) { - return; + return false; } context.Tags.Remove(tag); context.SaveChanges(); + + return true; } public TagDto GetTagById(int tagId) From 7784b40ae76b0713c20629ec1d77d578cbc2816e Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Tue, 7 May 2024 23:31:27 +0200 Subject: [PATCH 14/65] Updated services --- .../ReasnAPI/Services/InterestService.cs | 20 +++++++ .../ReasnAPI/Services/ParameterService.cs | 4 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 59 ++++++++++++++----- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 18277240..9a7688cf 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -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) { @@ -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; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 5923229f..d3116886 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -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; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 986c6904..9c41f73e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -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) @@ -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; + } + + 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; + } + + context.SaveChanges(); + return tagDto; } - tag.Name = tagDto.Name; - context.Tags.Update(tag); - context.SaveChanges(); - return tagDto; - } + } public bool DeleteTag(int tagId) { From ae26b70eca4ecfe5dd20f5be799b9ae39e23ff8a Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 27 May 2024 11:47:28 +0200 Subject: [PATCH 15/65] Updates after scafoled nr 1 --- .../ReasnAPI.Tests/Services/FakeDbSet.cs | 22 +- .../Services/ImageServiceTests.cs | 12 +- .../Services/InterestServiceTests.cs | 2 +- .../Services/StatusServiceTests.cs | 198 -------------- .../Services/TagServiceTests.cs | 16 +- .../ReasnAPI/Controllers/TestController.cs | 256 +++++++++++++++++- Server/ReasnAPI/ReasnAPI/Program.cs | 8 + .../ReasnAPI/Services/EventService.cs | 80 +++--- .../ReasnAPI/Services/ImageService.cs | 20 +- .../ReasnAPI/Services/InterestService.cs | 44 +-- .../ReasnAPI/Services/ParameterService.cs | 24 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 20 +- 12 files changed, 380 insertions(+), 322 deletions(-) delete mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs index d8c253a3..f8cb8506 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs @@ -1,4 +1,9 @@ -using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; namespace ReasnAPI.Tests.Services @@ -10,10 +15,10 @@ public class FakeDbSet : DbSet, IAsyncEnumerable public FakeDbSet() { - _data = []; + _data = new List(); } - public override T? Find(params object[] keyValues) + public override T Find(params object[] keyValues) { return _data.FirstOrDefault(); } @@ -24,7 +29,7 @@ public override EntityEntry Add(T entity) return null; // Return null for simplicity, adjust as needed } - public override IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { return new AsyncEnumerator(_data.GetEnumerator()); } @@ -33,9 +38,14 @@ public override IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancell // Implement other methods... - private class AsyncEnumerator(IEnumerator enumerator) : IAsyncEnumerator + private class AsyncEnumerator : IAsyncEnumerator { - private readonly IEnumerator _enumerator = enumerator; + private readonly IEnumerator _enumerator; + + public AsyncEnumerator(IEnumerator enumerator) + { + _enumerator = enumerator; + } public ValueTask DisposeAsync() { diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 5745330c..f3f3308b 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -89,9 +89,17 @@ public void GetAllImages_ImageNotExists_EmptyListReturned() [TestMethod] public void GetImageById_ImageExists_ImageReturned() { + + var imaDto = new ImageDto() + { + ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } + }; + + var fakeStatuses = new FakeDbSet(); + fakeStatuses.Add(new Image() { Id = 1, ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } }); + var mockContext = new Mock(); - mockContext.Setup(c => c.Images).ReturnsDbSet(new List - { new Image { Id = 1, ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } } }); + mockContext.Setup(c => c.Images).Returns(fakeStatuses); var imageService = new ImageService(mockContext.Object); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index 02559b86..352acd5e 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -95,7 +95,7 @@ public void GetInterestById_InterestDoesNotExist_NullReturned() public void GetInterestById_InterestExists_InterestReturned() { var mockContext = new Mock(); - mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Id = 1, Name = "TestInterest"} }); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest {Id = 1, Name = "TestInterest"} }); var interestService = new InterestService(mockContext.Object); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs deleted file mode 100644 index 7e59c56f..00000000 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/StatusServiceTests.cs +++ /dev/null @@ -1,198 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using ReasnAPI.Models.Database; -using ReasnAPI.Models.DTOs; -using ReasnAPI.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Linq; -using System.Linq.Expressions; -using Moq; -using Moq.EntityFrameworkCore; - -namespace ReasnAPI.Tests.Services -{ - [TestClass] - public class StatusServiceTests - { - [TestMethod] - public void CreateStatus_StatusDoesNotExist_StatusCreated() - { - var statusDto = new StatusDto - { - Name = "TestStatus" - }; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.CreateStatus(statusDto); - - Assert.AreEqual(statusDto.Name, result.Name); - } - - [TestMethod] - public void CreateStatus_StatusExists_StatusNotCreated() - { - var statusDto = new StatusDto - { - Name = "TestStatus" - }; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Name = "TestStatus" } }); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.CreateStatus(statusDto); - - Assert.IsNull(result); - } - - [TestMethod] - public void UpdateStatus_StatusExists_StatusUpdated() - { - var statusDto = new StatusDto - { - Name = "TestStatus" - }; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status {Id = 1, Name = "TestStatus" } }); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.UpdateStatus(1, statusDto); - - Assert.AreEqual(statusDto.Name, result.Name); - } - - [TestMethod] - public void UpdateStatus_StatusDoesNotExist_StatusNotUpdated() - { - var statusDto = new StatusDto - { - Name = "TestStatus" - }; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.UpdateStatus(1, statusDto); - - Assert.IsNull(result); - } - - [TestMethod] - public void GetStatusById_StatusDoesNotExist_NullReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.GetStatusById(1); - - Assert.IsNull(result); - } - - [TestMethod] - public void GetStatusById_StatusExists_StatusReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.GetStatusById(1); - - Assert.AreEqual("TestStatus", result.Name); - } - - [TestMethod] - public void DeleteStatus_StatusDoesNotExist_NothingDeleted() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); - var statusService = new StatusService(mockContext.Object); - - statusService.DeleteStatus(1); - - mockContext.Verify(c => c.SaveChanges(), Times.Never()); - } - - [TestMethod] - public void DeleteStatus_StatusExists_StatusDeleted() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); - mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); - var statusService = new StatusService(mockContext.Object); - - statusService.DeleteStatus(1); - - mockContext.Verify(c => c.SaveChanges(), Times.Once()); - } - - [TestMethod] - public void GetStatusByFilter_StatusExists_StatusReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.GetStatusesByFilter(s => s.Id == 1).ToList(); - - Assert.AreEqual(1, result.Count()); - } - - [TestMethod] - public void GetStatusByFilter_StatusDoesNotExist_NothingReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.GetStatusesByFilter(s => s.Id == 1).ToList(); - - Assert.AreEqual(0, result.Count()); - } - - [TestMethod] - public void GetAllStatuses_StatusExists_StatusReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { new Status { Id = 1, Name = "TestStatus" } }); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.GetAllStatuses().ToList(); - - Assert.AreEqual(1, result.Count()); - } - - [TestMethod] - public void GetAllStatuses_StatusDoesNotExist_NothingReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List()); - - var statusService = new StatusService(mockContext.Object); - - var result = statusService.GetAllStatuses().ToList(); - - Assert.AreEqual(0, result.Count()); - } - - } -} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 987dd6d1..1295316b 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -111,14 +111,16 @@ public void UpdateTag_TagExists_TagUpdated() { Name = "TestTag1" }; + var eventId = 1; var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event() { Id=1 } }); mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); - - var result = tagService.UpdateTag(1, tagDto); - + + var result = tagService.UpdateTag(1, tagDto, eventId); + Assert.AreEqual("TestTag1", result.Name); } @@ -129,16 +131,18 @@ public void UpdateTag_TagDoesNotExist_NullReturned() { Name = "TestTag" }; + var eventId = 1; var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); - - var result = tagService.UpdateTag(1, tagDto); - + + var result = tagService.UpdateTag(1, tagDto, eventId); + Assert.IsNull(result); } + [TestMethod] public void DeleteTag_TagExists_TagDeleted() diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs index a31e551d..ccf35895 100644 --- a/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs +++ b/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs @@ -1,5 +1,8 @@ -using Microsoft.AspNetCore.Http; +using System.Linq.Expressions; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using ReasnAPI.Services; @@ -10,25 +13,262 @@ namespace ReasnAPI.Controllers public class TestController : Controller { + private readonly EventService _eventService; + private readonly ImageService _imageService; + private readonly InterestService _interestService; + private readonly ParameterService _parameterService; private readonly TagService _tagService; - public TestController(TagService tagService) + public TestController(EventService eventService, ImageService imageService, InterestService interestService, ParameterService parameterService, TagService tagService) { + _eventService = eventService; + _imageService = imageService; + _interestService = interestService; + _parameterService = parameterService; _tagService = tagService; } [HttpGet] - public IActionResult Add() + [Route("event/{eventId}")] + public IActionResult GetEventById(int eventId) { + return Ok(_eventService.GetEventById(eventId)); + } + + [HttpGet] + [Route("event/filter")] + public IActionResult GetEventsByFilter([FromBody] Expression> filter) + { + return Ok(_eventService.GetEventsByFilter(filter)); + } + + [HttpGet] + [Route("event/all")] + public IActionResult GetAllEvents() + { + return Ok(_eventService.GetAllEvents()); + } - var tagDto = new TagDto + [HttpPost] + [Route("event/create")] + public IActionResult CreateEvent([FromBody] EventDto eventDto) + { + var @event = _eventService.CreateEvent(eventDto); + if (@event is null) { - Name = "TestTagasd" - }; + return StatusCode(StatusCodes.Status400BadRequest); + } + return StatusCode(StatusCodes.Status201Created); + } - var result = _tagService.CreateTag(tagDto); + [HttpPut] + [Route("event/update/{eventId}")] + public IActionResult UpdateEvent(int eventId, [FromBody] EventDto eventDto) + { + return Ok(_eventService.UpdateEvent(eventId, eventDto)); + } - return Ok(result); + [HttpDelete] + [Route("event/delete/{eventId}")] + public IActionResult DeleteEvent(int eventId) + { + _eventService.DeleteEvent(eventId); + return Ok(); + } + + [HttpPost] + [Route("image/create")] + public IActionResult CreateImage([FromBody] List imageDtos) + { + var image = _imageService.CreateImages(imageDtos); + if (image is null) + { + return StatusCode(StatusCodes.Status400BadRequest); + } + return StatusCode(StatusCodes.Status201Created); + } + + [HttpPut] + [Route("image/update/{imageId}")] + public IActionResult UpdateImage(int imageId, [FromBody] ImageDto imageDto) + { + return Ok(_imageService.UpdateImage(imageId, imageDto)); + } + + [HttpDelete] + [Route("image/delete/{imageId}")] + public IActionResult DeleteImage(int imageId) + { + _imageService.DeleteImage(imageId); + return Ok(); + } + + [HttpGet] + [Route("image/{imageId}")] + public IActionResult GetImageById(int imageId) + { + return Ok(_imageService.GetImageById(imageId)); + } + + [HttpGet] + [Route("image/filter")] + public IActionResult GetImagesByFilter([FromBody] Expression> filter) + { + return Ok(_imageService.GetImagesByFilter(filter)); + } + + + [HttpGet] + [Route("image/all")] + public IActionResult GetAllImages() + { + return Ok(_imageService.GetAllImages()); + } + + [HttpGet] + [Route("interest/{interestId}")] + public IActionResult GetInterestById(int interestId) + { + return Ok(_interestService.GetInterestById(interestId)); + } + + [HttpGet] + [Route("interest/all")] + public IActionResult GetAllInterests() + { + return Ok(_interestService.GetAllInterests()); + } + + [HttpGet] + [Route("interest/filter")] + public IActionResult GetInterestsByFilter([FromBody] Expression> filter) + { + return Ok(_interestService.GetInterestsByFilter(filter)); + } + + [HttpPost] + [Route("interest/create")] + public IActionResult CreateInterest([FromBody] InterestDto interestDto) + { + var interest = _interestService.CreateInterest(interestDto); + if (interest is null) + { + return StatusCode(StatusCodes.Status400BadRequest); + } + return StatusCode(StatusCodes.Status201Created); } + + [HttpPut] + [Route("interest/update/{interestId}")] + public IActionResult UpdateInterest(int interestId, [FromBody] InterestDto interestDto) + { + return Ok(_interestService.UpdateInterest(interestId, interestDto)); + } + + [HttpDelete] + [Route("interest/delete/{interestId}")] + public IActionResult DeleteInterest(int interestId) + { + _interestService.DeleteInterest(interestId); + return Ok(); + } + + [HttpGet] + [Route("parameter/{parameterId}")] + public IActionResult GetParameterById(int parameterId) + { + return Ok(_parameterService.GetParameterById(parameterId)); + } + + [HttpGet] + [Route("parameter/all")] + public IActionResult GetAllParameters() + { + return Ok(_parameterService.GetAllParameters()); + } + + [HttpGet] + [Route("parameter/filter")] + public IActionResult GetParametersByFilter([FromBody] Expression> filter) + { + return Ok(_parameterService.GetParametersByFilter(filter)); + } + + [HttpPost] + [Route("parameter/create")] + public IActionResult CreateParameter([FromBody] ParameterDto parameterDto) + { + var parameter = _parameterService.CreateParameter(parameterDto); + if (parameter is null) + { + return StatusCode(StatusCodes.Status400BadRequest); + } + return StatusCode(StatusCodes.Status201Created); + } + + [HttpPut] + [Route("parameter/update/{parameterId}")] + public IActionResult UpdateParameter(int parameterId, [FromBody] ParameterDto parameterDto) + { + return Ok(_parameterService.UpdateParameter(parameterId, parameterDto)); + } + + [HttpDelete] + [Route("parameter/delete/{parameterId}")] + public IActionResult DeleteParameter(int parameterId) + { + _parameterService.DeleteParameter(parameterId); + return Ok(); + } + + + [HttpGet] + [Route("tag/{tagId}")] + public IActionResult GetTagById(int tagId) + { + return Ok(_tagService.GetTagById(tagId)); + } + + [HttpGet] + [Route("tag/all")] + public IActionResult GetAllTags() + { + return Ok(_tagService.GetAllTags()); + } + + [HttpPost] + [Route("tag/create")] + public IActionResult CreateTag([FromBody] TagDto tagDto) + { + var tag = _tagService.CreateTag(tagDto); + if (tag is null) + { + return StatusCode(StatusCodes.Status400BadRequest); + } + return StatusCode(StatusCodes.Status201Created); + } + + [HttpGet] + [Route("tag/filter")] + public IActionResult GetTagsByFilter([FromBody] Expression> filter) + { + return Ok(_tagService.GetTagsByFilter(filter)); + } + + [HttpPut] + [Route("tag/update/{tagId}/{eventId}")] + public IActionResult UpdateTag(int tagId, [FromBody] TagDto tagDto, int eventId) + { + return Ok(_tagService.UpdateTag(tagId, tagDto, eventId)); + } + + [HttpDelete] + [Route("tag/delete/{tagId}")] + public IActionResult DeleteTag(int tagId) + { + _tagService.DeleteTag(tagId); + return Ok(); + } + } } diff --git a/Server/ReasnAPI/ReasnAPI/Program.cs b/Server/ReasnAPI/ReasnAPI/Program.cs index b7382108..3ba93ee0 100644 --- a/Server/ReasnAPI/ReasnAPI/Program.cs +++ b/Server/ReasnAPI/ReasnAPI/Program.cs @@ -23,6 +23,14 @@ builder.Services.AddDbContext(options => options.UseNpgsql(dataSource)); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + + builder.Services.AddControllers(); builder.Services.AddSwaggerGen(options => diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index dd22cb1d..ec5e9954 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -32,50 +32,36 @@ public EventDto CreateEvent(EventDto eventDto) context.SaveChanges(); var eventId = newEvent.Id; - if (eventDto.Tags == null) + if (eventDto.Tags is null) { return eventDto; } - foreach (var tag in eventDto.Tags) - { - var newTag = context.Tags.FirstOrDefault(r => r.Name == tag.Name); - if (newTag == null) - { - var tagToAdd = new Tag { Name = tag.Name }; - context.Tags.Add(tagToAdd); - context.SaveChanges(); - - var eventTag = new EventTag - { - EventId = eventId, - TagId = tagToAdd.Id - }; - context.EventTags.Add(eventTag); - context.SaveChanges(); - } - else - { - var eventTag = new EventTag - { - EventId = eventId, - TagId = newTag.Id - }; - context.EventTags.Add(eventTag); - context.SaveChanges(); - } - } + var newTags = eventDto.Tags + .Where(t => !context.Tags.Any(x => x.Name == t.Name)) + .Select(t => new Tag { Name = t.Name }) + .ToList(); + + context.Tags.AddRange(newTags); + context.SaveChanges(); + + var eventTagsToAdd = eventDto.Tags + .Select(t => new EventTag { EventId = eventId, TagId = context.Tags.First(x => x.Name == t.Name).Id }) + .ToList(); + + context.EventTags.AddRange(eventTagsToAdd); + context.SaveChanges(); } return eventDto; } - public EventDto UpdateEvent(int eventId, EventDto eventDto) + public EventDto? UpdateEvent(int eventId, EventDto eventDto) { using (var scope = new TransactionScope()) { var eventToUpdate = context.Events.FirstOrDefault(r => r.Id == eventId); - if (eventToUpdate == null) + if (eventToUpdate is null) { return null; } @@ -94,7 +80,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) var existingTags = context.EventTags.Where(r => r.EventId == eventId).Include(eventTag => eventTag.Tag).ToList(); - if (eventDto.Tags == null) + if (eventDto.Tags is null) { return eventDto; } @@ -118,19 +104,19 @@ where newTags.All(r => r.Name != existingTag.Tag.Name) where existingTags.All(r => r.Tag.Name != newTag.Name) select newTag; - foreach (var newTag in tagsToAdd) - { - var existingTag = context.Tags.FirstOrDefault(t => t.Name == newTag.Name); - if (existingTag == null) - { - existingTag = new Tag { Name = newTag.Name }; - context.Tags.Add(existingTag); - context.SaveChanges(); - } + var newTagsToAdd = tagsToAdd + .Where(t => !context.Tags.Any(x => x.Name == t.Name)) + .Select(t => new Tag { Name = t.Name }) + .ToList(); - context.EventTags.Add(new EventTag { EventId = eventId, TagId = existingTag.Id }); - } + context.Tags.AddRange(newTagsToAdd); + context.SaveChanges(); + + var eventTagsToAdd = newTagsToAdd + .Select(t => new EventTag { EventId = eventId, TagId = t.Id }) + .ToList(); + context.EventTags.AddRange(eventTagsToAdd); context.SaveChanges(); } @@ -143,7 +129,7 @@ public bool DeleteEvent(int eventId) { var eventToDelete = context.Events.FirstOrDefault(r => r.Id == eventId); - if (eventToDelete == null) + if (eventToDelete is null) { return false; } @@ -157,10 +143,10 @@ public bool DeleteEvent(int eventId) return true; } - public EventDto GetEventById(int eventId) + public EventDto? GetEventById(int eventId) { - var eventToReturn = context.Events.FirstOrDefault(r => r.Id == eventId); - if (eventToReturn == null) + var eventToReturn = context.Events.Find(eventId); + if (eventToReturn is null) { return null; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 6feeeef4..e3f9279d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -14,7 +14,7 @@ public List CreateImages(List imageDtos) foreach (var imageDto in imageDtos) { var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectTypeId == imageDto.ObjectTypeId); - if (image != null) + if (image is not null) { continue; } @@ -32,7 +32,7 @@ public List CreateImages(List imageDtos) if (newImages.Any()) { var objectType = context.ObjectTypes.FirstOrDefault(ot => ot.Id == newImages.First().ObjectTypeId); - if (objectType != null) + if (objectType is not null) { if (objectType.Name == "User" && newImages.Count > 1) { @@ -50,10 +50,10 @@ public List CreateImages(List imageDtos) return imageDtos; } - public ImageDto UpdateImage(int imageId, ImageDto imageDto) + public ImageDto? UpdateImage(int imageId, ImageDto imageDto) { var image = context.Images.FirstOrDefault(r => r.Id == imageId); - if (image == null) + if (image is null) { return null; } @@ -70,7 +70,7 @@ public ImageDto UpdateImage(int imageId, ImageDto imageDto) public bool DeleteImage(int id) { var image = context.Images.FirstOrDefault(r => r.Id == id); - if (image == null) + if (image is null) { return false; } @@ -80,10 +80,10 @@ public bool DeleteImage(int id) return true; } - public ImageDto GetImageById(int id) + public ImageDto? GetImageById(int id) { - var image = context.Images.FirstOrDefault(r => r.Id == id); - if (image == null) + var image = context.Images.Find(id); + if (image is null) { return null; } @@ -121,7 +121,7 @@ public IEnumerable GetImagesByFilter(Expression> fil ImageData = image.ImageData, ObjectId = image.ObjectId, ObjectTypeId = image.ObjectTypeId - }).ToList(); + }).AsEnumerable(); return imageDtos; } @@ -137,7 +137,7 @@ public IEnumerable GetImagesByEventIdAndType(int eventId) ImageData = image.ImageData, ObjectId = image.ObjectId, ObjectTypeId = image.ObjectTypeId - }).ToList(); + }).AsEnumerable(); return imageDtos; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 9a7688cf..cdfb4579 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -6,10 +6,10 @@ namespace ReasnAPI.Services; public class InterestService(ReasnContext context) { - public InterestDto CreateInterest(InterestDto interestDto) + public InterestDto? CreateInterest(InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Name == interestDto.Name); - if (interest != null) + if (interest is not null) { return null; } @@ -24,10 +24,10 @@ public InterestDto CreateInterest(InterestDto interestDto) return interestDto; } - public InterestDto UpdateInterest(int interestId, InterestDto interestDto) + public InterestDto? UpdateInterest(int interestId, InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); - if (interest == null) + if (interest is null) { return null; } @@ -43,12 +43,12 @@ 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) + if (eventInterest is not null) { return false; } - if (interest == null) + if (interest is null) { return false; } @@ -58,24 +58,24 @@ public bool DeleteInterest(int id) 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; - } + //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(); + // context.UserInterests.Remove(userInterest); + // context.SaveChanges(); - return true; - } + // return true; + //} - public InterestDto GetInterestById(int interestId) + public InterestDto? GetInterestById(int interestId) { - var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); - if (interest == null) + var interest = context.Interests.Find(interestId); + if (interest is null) { return null; } @@ -92,7 +92,7 @@ public IEnumerable GetAllInterests() { var interests = context.Interests.ToList(); - return interests.Select(interest => new InterestDto { Name = interest.Name }).ToList(); + return interests.Select(interest => new InterestDto { Name = interest.Name }).AsEnumerable(); } public IEnumerable GetInterestsByFilter(Expression> filter) @@ -102,7 +102,7 @@ public IEnumerable GetInterestsByFilter(Expression new InterestDto { Name = interest.Name - }).ToList(); + }).AsEnumerable(); return interestDtos; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index d3116886..9dd67911 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -5,10 +5,10 @@ namespace ReasnAPI.Services; public class ParameterService(ReasnContext context) { - public ParameterDto CreateParameter(ParameterDto parameterDto) + public ParameterDto? CreateParameter(ParameterDto parameterDto) { var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key && r.Value == parameterDto.Value); - if (parameter != null) + if (parameter is not null) { return null; } @@ -23,17 +23,17 @@ public ParameterDto CreateParameter(ParameterDto parameterDto) return parameterDto; } - public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) + public ParameterDto? UpdateParameter(int parameterId, ParameterDto parameterDto) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); - if (parameterCheck != null) // if parameter is associated with an event, it cannot be updated + if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated { return null; } - if (parameter == null) + if (parameter is null) { return null; } @@ -49,12 +49,12 @@ public bool DeleteParameter(int parameterId) var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); - if (parameterCheck != null) // if parameter is associated with an event, it cannot be deleted + if (parameterCheck is not null) // if parameter is associated with an event, it cannot be deleted { return false; } - if (parameter == null) + if (parameter is null) { return false; } @@ -63,10 +63,10 @@ public bool DeleteParameter(int parameterId) return true; } - public ParameterDto GetParameterById(int parameterId) + public ParameterDto? GetParameterById(int parameterId) { - var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); - if (parameter == null) + var parameter = context.Parameters.Find(parameterId); + if (parameter is null) { return null; } @@ -84,7 +84,7 @@ public IEnumerable GetAllParameters() { var parameters = context.Parameters.ToList(); - return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).ToList(); + return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).AsEnumerable(); } public IEnumerable GetParametersByFilter(Expression> filter) @@ -92,7 +92,7 @@ public IEnumerable GetParametersByFilter(Expression new ParameterDto { Key = parameter.Key, Value = parameter.Value }) - .ToList(); + .AsEnumerable(); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 9c41f73e..9a8b83d1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -7,10 +7,10 @@ namespace ReasnAPI.Services; public class TagService (ReasnContext context) { - public TagDto CreateTag(TagDto tagDto) + public TagDto? CreateTag(TagDto tagDto) { var tag = context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); - if (tag != null) + if (tag is not null) { return null; } @@ -24,13 +24,13 @@ public TagDto CreateTag(TagDto tagDto) return tagDto; } - public TagDto UpdateTag(int tagId, TagDto tagDto, int eventId) + public TagDto? UpdateTag(int tagId, TagDto tagDto, int eventId) { using (var scope = new TransactionScope()) { var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); - if (tag == null) + if (tag is null) { return null; } @@ -77,7 +77,7 @@ public bool DeleteTag(int tagId) 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 deleted + if (eventTag is not null) // if tag is associated with an event, it cannot be deleted { return false; } @@ -92,10 +92,10 @@ public bool DeleteTag(int tagId) return true; } - public TagDto GetTagById(int tagId) + public TagDto? GetTagById(int tagId) { - var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); - if(tag == null) + var tag = context.Tags.Find(tagId); + if(tag is null) { return null; } @@ -109,13 +109,13 @@ public TagDto GetTagById(int tagId) public IEnumerable GetAllTags() { var tags = context.Tags.ToList(); - return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); + return tags.Select(tag => new TagDto { Name = tag.Name }).AsEnumerable(); } public IEnumerable GetTagsByFilter(Expression> filter) { var tags = context.Tags.Where(filter).ToList(); - return tags.Select(tag => new TagDto { Name = tag.Name }).ToList(); + return tags.Select(tag => new TagDto { Name = tag.Name }).AsEnumerable(); } } \ No newline at end of file From fc48408441a1630282d24efb70f1c79c81949726 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 30 May 2024 16:12:59 +0200 Subject: [PATCH 16/65] Updated services Changed using bridge tables to using colections in EF. --- .../Services/EventServicesTest.cs | 23 +---- .../Services/ParameterServiceTests.cs | 4 - .../Services/TagServiceTests.cs | 4 - .../Models/Database/EventParameter.cs | 15 ---- .../ReasnAPI/Models/Database/EventTag.cs | 15 ---- .../ReasnAPI/Services/EventService.cs | 84 ++++++++----------- .../ReasnAPI/Services/ImageService.cs | 39 ++++----- .../ReasnAPI/Services/ParameterService.cs | 19 ++++- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 36 ++++---- 9 files changed, 92 insertions(+), 147 deletions(-) delete mode 100644 Server/ReasnAPI/ReasnAPI/Models/Database/EventParameter.cs delete mode 100644 Server/ReasnAPI/ReasnAPI/Models/Database/EventTag.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 24a77b23..f50b8314 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -8,6 +8,7 @@ using ReasnAPI.Models.Database; using Moq; using Moq.EntityFrameworkCore; +using ReasnAPI.Models.Enums; namespace ReasnAPI.Tests.Services @@ -36,21 +37,14 @@ public void CreateEvent_EventDoesNotExist_EventCreated() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, - StatusId = 1, + Status = EventStatus.Approved, Tags = tagList, }; var mockContext = new Mock(); mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Roles).ReturnsDbSet(new List - { - new Role() - { - Id = 1, - Name = "test" - } - }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { @@ -70,22 +64,13 @@ public void CreateEvent_EventDoesNotExist_EventCreated() AddressId = 1, CreatedAt = DateTime.Now, IsActive = true, - RoleId = 1, + Role = UserRole.Admin; Password ="test123", Phone = "123123123", Surname ="test", Username ="test", UpdatedAt =DateTime.Now }}); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List - { - new Status() - { - Id = 1, - Name = "TestStatus" - } - }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var eventService = new EventService(mockContext.Object); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index 2884b1df..de739e2c 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -65,7 +65,6 @@ public void UpdateParameter_ParameterExists_ParameterUpdated() var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); - mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); @@ -85,7 +84,6 @@ public void UpdateParameter_ParameterDoesNotExist_NullReturned() var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); - mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); var result = parameterService.UpdateParameter(1, parameterDto); @@ -124,7 +122,6 @@ public void DeleteParameter_ParameterExists_ParameterDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); - mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); parameterService.DeleteParameter(1); @@ -137,7 +134,6 @@ public void DeleteParameter_ParameterDoesNotExist_NothingDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); - mockContext.Setup(c => c.EventParameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); parameterService.DeleteParameter(1); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 1295316b..7fc2f665 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -116,7 +116,6 @@ public void UpdateTag_TagExists_TagUpdated() var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event() { Id=1 } }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); var result = tagService.UpdateTag(1, tagDto, eventId); @@ -135,7 +134,6 @@ public void UpdateTag_TagDoesNotExist_NullReturned() var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); var result = tagService.UpdateTag(1, tagDto, eventId); @@ -149,7 +147,6 @@ public void DeleteTag_TagExists_TagDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); tagService.DeleteTag(1); @@ -162,7 +159,6 @@ public void DeleteTag_TagDoesNotExist_NothingHappens() { var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); tagService.DeleteTag(1); diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/EventParameter.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/EventParameter.cs deleted file mode 100644 index 32fa03fc..00000000 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/EventParameter.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ReasnAPI.Models.Database; - -public partial class EventParameter -{ - public int ParameterId { get; set; } - - public int EventId { get; set; } - - public virtual Event Event { get; set; } = null!; - - public virtual Parameter Parameter { get; set; } = null!; -} diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/EventTag.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/EventTag.cs deleted file mode 100644 index e641f72d..00000000 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/EventTag.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ReasnAPI.Models.Database; - -public partial class EventTag -{ - public int EventId { get; set; } - - public int TagId { get; set; } - - public virtual Event Event { get; set; } = null!; - - public virtual Tag Tag { get; set; } = null!; -} diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index ec5e9954..ba5471b8 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -25,7 +25,7 @@ public EventDto CreateEvent(EventDto eventDto) CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, Slug = eventDto.Slug, - StatusId = eventDto.StatusId, + Status = eventDto.Status, }; context.Events.Add(newEvent); @@ -43,13 +43,10 @@ public EventDto CreateEvent(EventDto eventDto) .ToList(); context.Tags.AddRange(newTags); - context.SaveChanges(); - var eventTagsToAdd = eventDto.Tags - .Select(t => new EventTag { EventId = eventId, TagId = context.Tags.First(x => x.Name == t.Name).Id }) - .ToList(); + var addedEvent = context.Events.Find(eventId); + addedEvent.Tags = newTags; - context.EventTags.AddRange(eventTagsToAdd); context.SaveChanges(); } @@ -73,12 +70,19 @@ public EventDto CreateEvent(EventDto eventDto) eventToUpdate.StartAt = eventDto.StartAt; eventToUpdate.EndAt = eventDto.EndAt; eventToUpdate.UpdatedAt = DateTime.Now; - eventToUpdate.StatusId = eventDto.StatusId; + eventToUpdate.Status = eventDto.Status; context.Events.Update(eventToUpdate); context.SaveChanges(); - var existingTags = context.EventTags.Where(r => r.EventId == eventId).Include(eventTag => eventTag.Tag).ToList(); + var existingEvent = context.Events + .Include(e => e.Tags) + .FirstOrDefault(e => e.Id == eventId); + + if (existingEvent is null) + { + return null; + } if (eventDto.Tags is null) { @@ -91,33 +95,31 @@ public EventDto CreateEvent(EventDto eventDto) Name = tagDto.Name, }).ToList(); - var tagsToRemove = from existingTag in existingTags - where newTags.All(r => r.Name != existingTag.Tag.Name) - select existingTag; + var tagsToRemove = existingEvent.Tags + .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) + .ToList(); - foreach (var existingTag in tagsToRemove) + foreach (var tagToRemove in tagsToRemove) { - context.EventTags.Remove(existingTag); + existingEvent.Tags.Remove(tagToRemove); } - var tagsToAdd = from newTag in newTags - where existingTags.All(r => r.Tag.Name != newTag.Name) - select newTag; - - var newTagsToAdd = tagsToAdd - .Where(t => !context.Tags.Any(x => x.Name == t.Name)) - .Select(t => new Tag { Name = t.Name }) + var existingTagsInDb = context.Tags.ToList(); + var tagsToAdd = newTags + .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) .ToList(); - context.Tags.AddRange(newTagsToAdd); + context.Tags.AddRange(tagsToAdd); context.SaveChanges(); - var eventTagsToAdd = newTagsToAdd - .Select(t => new EventTag { EventId = eventId, TagId = t.Id }) + var updatedTags = newTags + .Select(newTag => existingTagsInDb.FirstOrDefault(existingTag => existingTag.Name == newTag.Name) ?? newTag) .ToList(); - context.EventTags.AddRange(eventTagsToAdd); + existingEvent.Tags = updatedTags; + context.SaveChanges(); + } return eventDto; @@ -134,9 +136,7 @@ public bool DeleteEvent(int eventId) return false; } - context.EventTags.RemoveRange(context.EventTags.Where(r => r.EventId == eventId)); context.Events.Remove(eventToDelete); - context.SaveChanges(); } @@ -145,19 +145,13 @@ public bool DeleteEvent(int eventId) public EventDto? GetEventById(int eventId) { - var eventToReturn = context.Events.Find(eventId); + var eventToReturn = context.Events.Include(e => e.Tags).FirstOrDefault(e => e.Id == eventId); if (eventToReturn is null) { return null; } - var tagIds = context.EventTags - .Where(r => r.EventId == eventId) - .Select(r => r.TagId) - .ToList(); - - var tags = context.Tags - .Where(t => tagIds.Contains(t.Id)) + var tags = eventToReturn.Tags .Select(t => new TagDto { Name = t.Name }) .ToList(); @@ -172,7 +166,7 @@ public bool DeleteEvent(int eventId) CreatedAt = eventToReturn.CreatedAt, UpdatedAt = eventToReturn.UpdatedAt, Slug = eventToReturn.Slug, - StatusId = eventToReturn.StatusId, + Status = eventToReturn.Status, Tags = tags }; @@ -185,13 +179,7 @@ public IEnumerable GetEventsByFilter(Expression> fil var eventDtos = new List(); foreach (var eventToReturn in events) { - var tagIds = context.EventTags - .Where(r => r.EventId == eventToReturn.Id) - .Select(r => r.TagId) - .ToList(); - - var tags = context.Tags - .Where(t => tagIds.Contains(t.Id)) + var tags = eventToReturn.Tags .Select(t => new TagDto { Name = t.Name }) .ToList(); @@ -206,7 +194,7 @@ public IEnumerable GetEventsByFilter(Expression> fil CreatedAt = eventToReturn.CreatedAt, UpdatedAt = eventToReturn.UpdatedAt, Slug = eventToReturn.Slug, - StatusId = eventToReturn.StatusId, + Status = eventToReturn.Status, Tags = tags }; @@ -222,13 +210,7 @@ public IEnumerable GetAllEvents() var eventDtos = new List(); foreach (var eventToReturn in events) { - var tagIds = context.EventTags - .Where(r => r.EventId == eventToReturn.Id) - .Select(r => r.TagId) - .ToList(); - - var tags = context.Tags - .Where(t => tagIds.Contains(t.Id)) + var tags = eventToReturn.Tags .Select(t => new TagDto { Name = t.Name }) .ToList(); @@ -243,7 +225,7 @@ public IEnumerable GetAllEvents() CreatedAt = eventToReturn.CreatedAt, UpdatedAt = eventToReturn.UpdatedAt, Slug = eventToReturn.Slug, - StatusId = eventToReturn.StatusId, + Status = eventToReturn.Status, Tags = tags }; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index e3f9279d..1ee5389f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -1,6 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -using System.Linq.Expressions; +using System.Linq.Expressions; +using ReasnAPI.Models.Enums; using static System.Net.Mime.MediaTypeNames; using Image = ReasnAPI.Models.Database.Image; @@ -13,7 +14,7 @@ public List CreateImages(List imageDtos) foreach (var imageDto in imageDtos) { - var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectTypeId == imageDto.ObjectTypeId); + var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectType == imageDto.ObjectType); if (image is not null) { continue; @@ -23,7 +24,7 @@ public List CreateImages(List imageDtos) { ImageData = imageDto.ImageData, ObjectId = imageDto.ObjectId, - ObjectTypeId = imageDto.ObjectTypeId + ObjectType = imageDto.ObjectType }; newImages.Add(newImage); @@ -31,18 +32,18 @@ public List CreateImages(List imageDtos) if (newImages.Any()) { - var objectType = context.ObjectTypes.FirstOrDefault(ot => ot.Id == newImages.First().ObjectTypeId); - if (objectType is not null) + var objectType = newImages.First().ObjectType; + + + if (objectType == ObjectType.User && newImages.Count == 1) { - if (objectType.Name == "User" && newImages.Count > 1) - { - context.Images.Add(newImages.First()); - } - else if (objectType.Name == "Event") - { - context.Images.AddRange(newImages); - } + context.Images.Add(newImages.First()); } + else if (objectType == ObjectType.Event) + { + context.Images.AddRange(newImages); + } + context.SaveChanges(); } @@ -60,7 +61,7 @@ public List CreateImages(List imageDtos) image.ObjectId = imageDto.ObjectId; image.ImageData = imageDto.ImageData; - image.ObjectTypeId = imageDto.ObjectTypeId; + image.ObjectType = imageDto.ObjectType; context.Images.Update(image); context.SaveChanges(); @@ -92,7 +93,7 @@ public bool DeleteImage(int id) { ImageData = image.ImageData, ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId + ObjectType = image.ObjectType }; return imageDto; @@ -106,7 +107,7 @@ public IEnumerable GetAllImages() { ImageData = image.ImageData, ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId + ObjectType = image.ObjectType }).ToList(); return imageDtos; @@ -120,7 +121,7 @@ public IEnumerable GetImagesByFilter(Expression> fil { ImageData = image.ImageData, ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId + ObjectType = image.ObjectType }).AsEnumerable(); return imageDtos; @@ -129,14 +130,14 @@ public IEnumerable GetImagesByFilter(Expression> fil public IEnumerable GetImagesByEventIdAndType(int eventId) { var images = context.Images - .Where(image => image.ObjectType.Name == "event" && image.ObjectId == eventId) + .Where(image => image.ObjectType == ObjectType.Event && image.ObjectId == eventId) .ToList(); var imageDtos = images.Select(image => new ImageDto { ImageData = image.ImageData, ObjectId = image.ObjectId, - ObjectTypeId = image.ObjectTypeId + ObjectType = image.ObjectType }).AsEnumerable(); return imageDtos; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 9dd67911..0fdca0c7 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -1,6 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; namespace ReasnAPI.Services; public class ParameterService(ReasnContext context) @@ -27,7 +28,10 @@ public class ParameterService(ReasnContext context) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); - var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); + var parameters = context.Events.Include(p => p.Parameters); + + var parameterCheck = parameters.FirstOrDefault(r => r.Parameters.Any(p => p.Id == parameterId)); + if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated { return null; @@ -48,16 +52,23 @@ public bool DeleteParameter(int parameterId) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); - var parameterCheck = context.EventParameters.FirstOrDefault(r => r.ParameterId == parameterId); - if (parameterCheck is not null) // if parameter is associated with an event, it cannot be deleted + if (parameter == null) { return false; } - if (parameter is null) + var eventsWithParameters = context.Events + .Include(e => e.Parameters) + .ToList(); + + var parameterCheck = eventsWithParameters + .Any(e => e.Parameters.Any(p => p.Id == parameterId)); + + if (parameterCheck) { return false; } + context.Parameters.Remove(parameter); context.SaveChanges(); return true; diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 9a8b83d1..94d14785 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Linq.Expressions; using System.Transactions; +using Microsoft.EntityFrameworkCore; namespace ReasnAPI.Services; public class TagService (ReasnContext context) @@ -35,10 +36,12 @@ public class TagService (ReasnContext context) return null; } - 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 + var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); + + var eventTags = eventsWithTags.Where(e => e.Tags.Any(t => t.Id == tagId) && e.Id != eventId).ToList(); + if (eventTags.Any()) // if tag is associated with more than one event { - // Create new tag and event tag, and remove the old one + // Create new tag, associate it with the event, and remove the old association var newTag = new Tag { Name = tagDto.Name @@ -46,18 +49,14 @@ public class TagService (ReasnContext context) context.Tags.Add(newTag); context.SaveChanges(); - var newEventTag = new EventTag + var eventToUpdate = eventsWithTags.FirstOrDefault(e => e.Id == eventId); + if (eventToUpdate != null) { - 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); + eventToUpdate.Tags.Remove(tag); + eventToUpdate.Tags.Add(newTag); + } } - else if (eventTags.Count == 1 && - eventTags[0].EventId == eventId) // if tag is associated only with the same event + else if (eventTags.Count == 1 && eventTags[0].Id == eventId) // if tag is associated only with the same event { tag.Name = tagDto.Name; context.Tags.Update(tag); @@ -68,6 +67,7 @@ public class TagService (ReasnContext context) } context.SaveChanges(); + scope.Complete(); return tagDto; } } @@ -76,16 +76,20 @@ public bool DeleteTag(int tagId) { var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); - var eventTag = context.EventTags.FirstOrDefault(r => r.TagId == tagId); - if (eventTag is not null) // if tag is associated with an event, it cannot be deleted + if (tag == null) { return false; } - if (tag == null) + var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); + + var isTagAssociatedWithEvent = eventsWithTags.Any(e => e.Tags.Any(t => t.Id == tagId)); + + if (isTagAssociatedWithEvent) { return false; } + context.Tags.Remove(tag); context.SaveChanges(); From ad78d8b6254df1282a5f12173c12c0c749d400d6 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 30 May 2024 20:16:34 +0200 Subject: [PATCH 17/65] Updated EventService added a logic for parameters in event --- .../Services/EventServicesTest.cs | 2 +- .../ReasnAPI/Services/EventService.cs | 151 +++++++++++++----- 2 files changed, 109 insertions(+), 44 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index f50b8314..c764061f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -64,7 +64,7 @@ public void CreateEvent_EventDoesNotExist_EventCreated() AddressId = 1, CreatedAt = DateTime.Now, IsActive = true, - Role = UserRole.Admin; + Role = UserRole.Admin, Password ="test123", Phone = "123123123", Surname ="test", diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index ba5471b8..479c3079 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -8,12 +8,12 @@ namespace ReasnAPI.Services; public class EventService(ReasnContext context) { - public EventDto CreateEvent(EventDto eventDto) + public EventDto? CreateEvent(EventDto eventDto) { using (var scope = new TransactionScope()) { eventDto.Slug = CreateSlug(eventDto); - + var nowTime = DateTime.Now; var newEvent = new Event { Name = eventDto.Name, @@ -22,8 +22,8 @@ public EventDto CreateEvent(EventDto eventDto) OrganizerId = eventDto.OrganizerId, StartAt = eventDto.StartAt, EndAt = eventDto.EndAt, - CreatedAt = DateTime.Now, - UpdatedAt = DateTime.Now, + CreatedAt = nowTime, + UpdatedAt = nowTime, Slug = eventDto.Slug, Status = eventDto.Status, }; @@ -32,20 +32,33 @@ public EventDto CreateEvent(EventDto eventDto) context.SaveChanges(); var eventId = newEvent.Id; - if (eventDto.Tags is null) + + var addedEvent = context.Events.Include(e => e.Tags).Include(e => e.Parameters) + .FirstOrDefault(e => e.Id == eventId); + + if (addedEvent == null) return null; + if (eventDto.Tags is not null && eventDto.Tags.Count > 0) { - return eventDto; + var newTags = eventDto.Tags + .Where(t => !context.Tags.Any(x => x.Name == t.Name)) + .Select(t => new Tag { Name = t.Name }) + .ToList(); + + context.Tags.AddRange(newTags); + addedEvent.Tags = newTags; } - var newTags = eventDto.Tags - .Where(t => !context.Tags.Any(x => x.Name == t.Name)) - .Select(t => new Tag { Name = t.Name }) - .ToList(); + if (eventDto.Parameters is not null && eventDto.Parameters.Count > 0) + { + var newParameters = eventDto.Parameters + .Where(p => !context.Parameters.Any(x => x.Key == p.Key && x.Value == p.Value)) + .Select(p => new Parameter { Key = p.Key, Value = p.Value }) + .ToList(); - context.Tags.AddRange(newTags); + context.Parameters.AddRange(newParameters); - var addedEvent = context.Events.Find(eventId); - addedEvent.Tags = newTags; + addedEvent.Parameters = newParameters; + } context.SaveChanges(); } @@ -84,41 +97,77 @@ public EventDto CreateEvent(EventDto eventDto) return null; } - if (eventDto.Tags is null) + if (eventDto.Tags is not null) { - return eventDto; - } - var newTags = eventDto.Tags - .Select(tagDto => new Tag + var newTags = eventDto.Tags + .Select(tagDto => new Tag + { + Name = tagDto.Name, + }).ToList(); + + var tagsToRemove = existingEvent.Tags + .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) + .ToList(); + + foreach (var tagToRemove in tagsToRemove) { - Name = tagDto.Name, - }).ToList(); + existingEvent.Tags.Remove(tagToRemove); + } - var tagsToRemove = existingEvent.Tags - .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) - .ToList(); + var existingTagsInDb = context.Tags.ToList(); + var tagsToAdd = newTags + .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) + .ToList(); - foreach (var tagToRemove in tagsToRemove) - { - existingEvent.Tags.Remove(tagToRemove); + context.Tags.AddRange(tagsToAdd); + context.SaveChanges(); + + var updatedTags = newTags + .Select(newTag => existingTagsInDb.FirstOrDefault(existingTag => existingTag.Name == newTag.Name) ?? + newTag) + .ToList(); + + existingEvent.Tags = updatedTags; + + context.SaveChanges(); } - var existingTagsInDb = context.Tags.ToList(); - var tagsToAdd = newTags - .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); + if (eventDto.Parameters is not null) + { + var newParameters = eventDto.Parameters + .Select(paramDto => new Parameter + { + Key = paramDto.Key, + Value = paramDto.Value + }).ToList(); + + var paramsToRemove = existingEvent.Parameters + .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key)) + .ToList(); + + foreach (var paramToRemove in paramsToRemove) + { + existingEvent.Parameters.Remove(paramToRemove); + } - context.Tags.AddRange(tagsToAdd); - context.SaveChanges(); + var existingParamsInDb = context.Parameters.ToList(); + var paramsToAdd = newParameters + .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key)) + .ToList(); - var updatedTags = newTags - .Select(newTag => existingTagsInDb.FirstOrDefault(existingTag => existingTag.Name == newTag.Name) ?? newTag) - .ToList(); + context.Parameters.AddRange(paramsToAdd); + context.SaveChanges(); - existingEvent.Tags = updatedTags; + var updatedParams = newParameters + .Select(newParam => existingParamsInDb.FirstOrDefault(existingParam => existingParam.Key == newParam.Key) ?? + newParam) + .ToList(); - context.SaveChanges(); + existingEvent.Parameters = updatedParams; + + context.SaveChanges(); + } } @@ -145,7 +194,7 @@ public bool DeleteEvent(int eventId) public EventDto? GetEventById(int eventId) { - var eventToReturn = context.Events.Include(e => e.Tags).FirstOrDefault(e => e.Id == eventId); + var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Id == eventId); if (eventToReturn is null) { return null; @@ -154,6 +203,10 @@ public bool DeleteEvent(int eventId) var tags = eventToReturn.Tags .Select(t => new TagDto { Name = t.Name }) .ToList(); + + var parameters = eventToReturn.Parameters + .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) + .ToList(); var eventDto = new EventDto { @@ -167,7 +220,8 @@ public bool DeleteEvent(int eventId) UpdatedAt = eventToReturn.UpdatedAt, Slug = eventToReturn.Slug, Status = eventToReturn.Status, - Tags = tags + Tags = tags, + Parameters = parameters }; return eventDto; @@ -175,7 +229,7 @@ public bool DeleteEvent(int eventId) public IEnumerable GetEventsByFilter(Expression> filter) { - var events = context.Events.Where(filter).ToList(); + var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Where(filter).ToList(); var eventDtos = new List(); foreach (var eventToReturn in events) { @@ -183,6 +237,10 @@ public IEnumerable GetEventsByFilter(Expression> fil .Select(t => new TagDto { Name = t.Name }) .ToList(); + var parameters = eventToReturn.Parameters + .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) + .ToList(); + var eventDto = new EventDto { Name = eventToReturn.Name, @@ -195,7 +253,8 @@ public IEnumerable GetEventsByFilter(Expression> fil UpdatedAt = eventToReturn.UpdatedAt, Slug = eventToReturn.Slug, Status = eventToReturn.Status, - Tags = tags + Tags = tags, + Parameters = parameters }; eventDtos.Add(eventDto); @@ -206,7 +265,8 @@ public IEnumerable GetEventsByFilter(Expression> fil public IEnumerable GetAllEvents() { - var events = context.Events.ToList(); + var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).ToList(); + var eventDtos = new List(); foreach (var eventToReturn in events) { @@ -214,6 +274,10 @@ public IEnumerable GetAllEvents() .Select(t => new TagDto { Name = t.Name }) .ToList(); + var parameters = eventToReturn.Parameters + .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) + .ToList(); + var eventDto = new EventDto { Name = eventToReturn.Name, @@ -226,7 +290,8 @@ public IEnumerable GetAllEvents() UpdatedAt = eventToReturn.UpdatedAt, Slug = eventToReturn.Slug, Status = eventToReturn.Status, - Tags = tags + Tags = tags, + Parameters = parameters }; eventDtos.Add(eventDto); From f19e208e0bbd7ae8fc706713506bf5ce37e410b7 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 6 Jun 2024 16:25:50 +0200 Subject: [PATCH 18/65] working event services --- .../Services/EventServicesTest.cs | 258 ++++++----------- .../Services/InterestServiceTests.cs | 63 +++- .../Services/ParameterServiceTests.cs | 95 ++++-- .../Services/TagServiceTests.cs | 97 +++++-- .../ReasnAPI/Controllers/TestController.cs | 274 ------------------ .../ReasnAPI/Models/Database/Event.cs | 5 +- .../ReasnAPI/Models/Database/ReasnContext.cs | 7 +- Server/ReasnAPI/ReasnAPI/Program.cs | 4 +- .../ReasnAPI/Services/EventService.cs | 9 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 10 +- 10 files changed, 306 insertions(+), 516 deletions(-) delete mode 100644 Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index c764061f..0b7b17a6 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -16,69 +16,78 @@ namespace ReasnAPI.Tests.Services [TestClass] public class EventServicesTest { - [TestMethod] - public void CreateEvent_EventDoesNotExist_EventCreated() - { - var tagDto = new TagDto() - { - Name = "tesTag" - }; - var tagList = new List - { - tagDto - }; - var eventDto = new EventDto() - { - Name = "name", - AddressId = 1, - Description = "description", - OrganizerId = 1, - StartAt = DateTime.Now, - EndAt = DateTime.Now, - CreatedAt = DateTime.Now, - UpdatedAt = DateTime.Now, - Status = EventStatus.Approved, - Tags = tagList, - }; + // [TestMethod] + // public void CreateEvent_EventDoesNotExist_EventCreated() + // { + // // Arrange + // var tagDto = new TagDto + // { + // Name = "testTag" + // }; + // var tagList = new List { tagDto }; - var mockContext = new Mock(); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - - mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ - new Address() - { - Id = 1, - City = "city", - Country = "country", - State = "state", - Street = "street", - ZipCode = "test123" - }}); - mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ - new User() - { - Id = 1, - Name = "test", - Email = "test@wp.pl", - AddressId = 1, - CreatedAt = DateTime.Now, - IsActive = true, - Role = UserRole.Admin, - Password ="test123", - Phone = "123123123", - Surname ="test", - Username ="test", - UpdatedAt =DateTime.Now - }}); + // var eventDto = new EventDto + // { + // Name = "name", + // AddressId = 1, + // Description = "description", + // OrganizerId = 1, + // StartAt = DateTime.Now, + // EndAt = DateTime.Now, + // CreatedAt = DateTime.Now, + // UpdatedAt = DateTime.Now, + // Status = EventStatus.Approved, + // Tags = tagList, + // }; - var eventService = new EventService(mockContext.Object); + // var mockContext = new Mock(); + // var events = new List(); + // var tags = new List(); - var result = eventService.CreateEvent(eventDto); - Assert.IsNotNull(result); - Assert.AreEqual(1,result.Tags.Count); - - } + // mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + // mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); + // mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + + // mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
+ //{ + // new Address + // { + // Id = 1, + // City = "city", + // Country = "country", + // State = "state", + // Street = "street", + // ZipCode = "test123" + // } + //}); + + // mockContext.Setup(c => c.Users).ReturnsDbSet(new List + //{ + // new User + // { + // Id = 1, + // Name = "test", + // Email = "test@wp.pl", + // AddressId = 1, + // CreatedAt = DateTime.Now, + // IsActive = true, + // Role = UserRole.Admin, + // Password = "test123", + // Phone = "123123123", + // Surname = "test", + // Username = "test", + // UpdatedAt = DateTime.Now + // } + //}); + + // var eventService = new EventService(mockContext.Object); + + // // Act + // var result = eventService.CreateEvent(eventDto); + + // // Assert + // Assert.IsNotNull(result); + // } [TestMethod] public void UpdateEvent_EventExists_EventUpdated() @@ -98,7 +107,7 @@ public void UpdateEvent_EventExists_EventUpdated() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, - StatusId = 1, + Status = EventStatus.Completed, Tags = tagList, }; @@ -115,17 +124,10 @@ public void UpdateEvent_EventExists_EventUpdated() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, - StatusId = 1, + Status = EventStatus.Completed, }}); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Roles).ReturnsDbSet(new List - { - new Role() - { - Id = 1, - Name = "test" - } - }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { @@ -144,19 +146,13 @@ public void UpdateEvent_EventExists_EventUpdated() Email = "test@wp.pl", AddressId = 1, CreatedAt = DateTime.Now, - IsActive = true, RoleId = 1, + IsActive = true, Role = UserRole.Admin, Password ="test123", Phone = "123123123", Surname ="test", Username ="test", UpdatedAt =DateTime.Now }}); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List { - new Status() - { - Id = 1, - Name = "TestStatus" - } }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + var eventService = new EventService(mockContext.Object); var result = eventService.UpdateEvent(1,eventDto); @@ -184,21 +180,14 @@ public void UpdateEvent_EventDoesNotExist_NullReturned() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, - StatusId = 1, + Status = EventStatus.Completed, Tags = tagList, }; var mockContext = new Mock(); mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Roles).ReturnsDbSet(new List - { - new Role() - { - Id = 1, - Name = "test" - } - }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { @@ -218,22 +207,14 @@ public void UpdateEvent_EventDoesNotExist_NullReturned() AddressId = 1, CreatedAt = DateTime.Now, IsActive = true, - RoleId = 1, + Role = UserRole.Admin, Password ="test123", Phone = "123123123", Surname ="test", Username ="test", UpdatedAt =DateTime.Now }}); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List - { - new Status() - { - Id = 1, - Name = "TestStatus" - } - }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + var eventService = new EventService(mockContext.Object); var result = eventService.UpdateEvent(1, eventDto); @@ -259,7 +240,7 @@ public void GetEventById_EventExists_EventReturned() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, - StatusId = 1, + Status = EventStatus.Completed, }}); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { @@ -269,14 +250,7 @@ public void GetEventById_EventExists_EventReturned() Name = "name" } }); - mockContext.Setup(c => c.Roles).ReturnsDbSet(new List - { - new Role() - { - Id = 1, - Name = "test" - } - }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { @@ -296,29 +270,13 @@ public void GetEventById_EventExists_EventReturned() AddressId = 1, CreatedAt = DateTime.Now, IsActive = true, - RoleId = 1, + Role = UserRole.User, Password ="test123", Phone = "123123123", Surname ="test", Username ="test", UpdatedAt =DateTime.Now }}); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List - { - new Status() - { - Id = 1, - Name = "TestStatus" - } - }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List - { - new EventTag() - { - EventId = 1, - TagId = 1 - } - - }); + var eventService = new EventService(mockContext.Object); @@ -334,14 +292,7 @@ public void GetEventById_EventDoesNotExist_NullReturned() var mockContext = new Mock(); mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Roles).ReturnsDbSet(new List - { - new Role() - { - Id = 1, - Name = "test" - } - }); + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() @@ -363,22 +314,14 @@ public void GetEventById_EventDoesNotExist_NullReturned() AddressId = 1, CreatedAt = DateTime.Now, IsActive = true, - RoleId = 1, + Role = UserRole.Admin, Password ="test123", Phone = "123123123", Surname ="test", Username ="test", UpdatedAt =DateTime.Now }}); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List - { - new Status() - { - Id = 1, - Name = "TestStatus" - } - }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List()); + var eventService = new EventService(mockContext.Object); @@ -406,7 +349,7 @@ public void DeleteEvent_EventExists_EventDeleted() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, - StatusId = 1, + Status = EventStatus.Completed, } }); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List @@ -417,14 +360,6 @@ public void DeleteEvent_EventExists_EventDeleted() Name = "name" } }); - mockContext.Setup(c => c.Roles).ReturnsDbSet(new List - { - new Role() - { - Id = 1, - Name = "test" - } - }); mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() @@ -446,29 +381,14 @@ public void DeleteEvent_EventExists_EventDeleted() AddressId = 1, CreatedAt = DateTime.Now, IsActive = true, - RoleId = 1, + Role = UserRole.User, Password ="test123", Phone = "123123123", Surname ="test", Username ="test", UpdatedAt =DateTime.Now }}); - mockContext.Setup(c => c.Statuses).ReturnsDbSet(new List - { - new Status() - { - Id = 1, - Name = "TestStatus" - } - }); - mockContext.Setup(c => c.EventTags).ReturnsDbSet(new List - { - new EventTag() - { - EventId = 1, - TagId = 1 - } - - }); + + var eventService = new EventService(mockContext.Object); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index 352acd5e..62b38786 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -91,18 +91,18 @@ public void GetInterestById_InterestDoesNotExist_NullReturned() Assert.IsNull(result); } - [TestMethod] - public void GetInterestById_InterestExists_InterestReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest {Id = 1, Name = "TestInterest"} }); + //[TestMethod] + //public void GetInterestById_InterestExists_InterestReturned() + //{ + // var mockContext = new Mock(); + // mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest {Id = 1, Name = "TestInterest"} }); - var interestService = new InterestService(mockContext.Object); + // var interestService = new InterestService(mockContext.Object); - var result = interestService.GetInterestById(1); + // var result = interestService.GetInterestById(1); - Assert.AreEqual("TestInterest", result.Name); - } + // Assert.AreEqual("TestInterest", result.Name); + //} [TestMethod] public void UpdateInterest_InterestExists_InterestUpdated() @@ -144,12 +144,18 @@ public void UpdateInterest_InterestDoesNotExist_NullReturned() public void DeleteInterest_InterestExists_InterestDeleted() { var mockContext = new Mock(); - mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Id = 1, Name = "TestInterest" } }); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List + { + new Interest { Id = 1, Name = "TestInterest" } + }); + mockContext.Setup(c => c.UserInterests).ReturnsDbSet(new List()); var interestService = new InterestService(mockContext.Object); - - interestService.DeleteInterest(1); - + + var result = interestService.DeleteInterest(1); + + Assert.IsTrue(result); + mockContext.Verify(c => c.Interests.Remove(It.Is(i => i.Id == 1)), Times.Once); mockContext.Verify(c => c.SaveChanges(), Times.Once); } @@ -158,11 +164,36 @@ public void DeleteInterest_InterestDoesNotExist_NothingDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); + mockContext.Setup(c => c.UserInterests).ReturnsDbSet(new List()); var interestService = new InterestService(mockContext.Object); - - interestService.DeleteInterest(1); - + + var result = interestService.DeleteInterest(1); + + Assert.IsFalse(result); + mockContext.Verify(c => c.Interests.Remove(It.IsAny()), Times.Never); + mockContext.Verify(c => c.SaveChanges(), Times.Never); + } + + [TestMethod] + public void DeleteInterest_InterestHasUserInterests_NothingDeleted() + { + var mockContext = new Mock(); + mockContext.Setup(c => c.Interests).ReturnsDbSet(new List + { + new Interest { Id = 1, Name = "TestInterest" } + }); + mockContext.Setup(c => c.UserInterests).ReturnsDbSet(new List + { + new UserInterest { InterestId = 1, UserId = 1 } + }); + + var interestService = new InterestService(mockContext.Object); + + var result = interestService.DeleteInterest(1); + + Assert.IsFalse(result); + mockContext.Verify(c => c.Interests.Remove(It.IsAny()), Times.Never); mockContext.Verify(c => c.SaveChanges(), Times.Never); } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index de739e2c..5b3a065e 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -57,25 +57,35 @@ public void CreateParameter_ParameterExists_ParameterNotCreated() [TestMethod] public void UpdateParameter_ParameterExists_ParameterUpdated() { + // Arrange var parameterDto = new ParameterDto { - Key = "TestKey", - Value = "TestValue" + Key = "UpdatedKey", + Value = "UpdatedValue" }; + var existingParameter = new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" }; var mockContext = new Mock(); - mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { existingParameter }); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); // No events associated var parameterService = new ParameterService(mockContext.Object); - + + // Act var result = parameterService.UpdateParameter(1, parameterDto); - + + // Assert + Assert.IsNotNull(result); Assert.AreEqual(parameterDto.Value, result.Value); + Assert.AreEqual(parameterDto.Key, result.Key); + Assert.AreEqual(parameterDto.Value, existingParameter.Value); // Verify the parameter was updated + mockContext.Verify(c => c.SaveChanges(), Times.Once); // Ensure SaveChanges was called } [TestMethod] public void UpdateParameter_ParameterDoesNotExist_NullReturned() { + // Arrange var parameterDto = new ParameterDto { Key = "TestKey", @@ -83,49 +93,94 @@ public void UpdateParameter_ParameterDoesNotExist_NullReturned() }; var mockContext = new Mock(); - mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); // No parameters in context + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); // No events associated + var parameterService = new ParameterService(mockContext.Object); - + + // Act var result = parameterService.UpdateParameter(1, parameterDto); - + + // Assert Assert.IsNull(result); + mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } [TestMethod] - public void GetParameterById_ParameterDoesNotExist_NullReturned() + public void UpdateParameter_ParameterInUse_NullReturned() { + // Arrange + var parameterDto = new ParameterDto + { + Key = "UpdatedKey", + Value = "UpdatedValue" + }; + + var existingParameter = new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" }; var mockContext = new Mock(); - mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { existingParameter }); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List + { + new Event { Parameters = new List { existingParameter } } + }); // Parameter is associated with an event var parameterService = new ParameterService(mockContext.Object); - - var result = parameterService.GetParameterById(1); - + + // Act + var result = parameterService.UpdateParameter(1, parameterDto); + + // Assert Assert.IsNull(result); + mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } [TestMethod] - public void GetParameterById_ParameterExists_ParameterReturned() + public void GetParameterById_ParameterDoesNotExist_NullReturned() { var mockContext = new Mock(); - mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); var result = parameterService.GetParameterById(1); - Assert.AreEqual("TestValue", result.Value); + Assert.IsNull(result); } + //[TestMethod] + //public void GetParameterById_ParameterExists_ParameterReturned() + //{ + // var mockContext = new Mock(); + // mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + + // var parameterService = new ParameterService(mockContext.Object); + + // var result = parameterService.GetParameterById(1); + + // Assert.AreEqual("TestValue", result.Value); + //} + [TestMethod] public void DeleteParameter_ParameterExists_ParameterDeleted() { + // Arrange + var parameters = new List + { + new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } + }; + var events = new List(); + var mockContext = new Mock(); - mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(parameters); + mockContext.Setup(c => c.Events).ReturnsDbSet(events); + var parameterService = new ParameterService(mockContext.Object); - - parameterService.DeleteParameter(1); - + + // Act + var result = parameterService.DeleteParameter(1); + + // Assert + Assert.IsTrue(result); mockContext.Verify(c => c.SaveChanges(), Times.Once); } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 7fc2f665..dd6eed32 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -78,18 +78,18 @@ public void GetAllTags_TagNotExists_EmptyListReturned() Assert.AreEqual(0, result.Count); } - [TestMethod] - public void GetTagById_TagExists_TagReturned() - { - var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + //[TestMethod] + //public void GetTagById_TagExists_TagReturned() + //{ + // var mockContext = new Mock(); + // mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); - var tagService = new TagService(mockContext.Object); + // var tagService = new TagService(mockContext.Object); - var result = tagService.GetTagById(1); + // var result = tagService.GetTagById(1); - Assert.AreEqual("TestTag", result.Name); - } + // Assert.AreEqual("TestTag", result.Name); + //} [TestMethod] public void GetTagById_TagDoesNotExist_NullReturned() @@ -107,63 +107,112 @@ public void GetTagById_TagDoesNotExist_NullReturned() [TestMethod] public void UpdateTag_TagExists_TagUpdated() { + // Arrange var tagDto = new TagDto { Name = "TestTag1" }; + var tag = new Tag { Id = 1, Name = "TestTag" }; var eventId = 1; var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event() { Id=1 } }); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { tag }); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId, Tags = new List { tag } } }); + var tagService = new TagService(mockContext.Object); + // Act var result = tagService.UpdateTag(1, tagDto, eventId); - Assert.AreEqual("TestTag1", result.Name); + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(tagDto.Name, result.Name); + Assert.AreEqual(tagDto.Name, tag.Name); // Verify the tag was updated + mockContext.Verify(c => c.SaveChanges(), Times.Once); // Ensure SaveChanges was called } [TestMethod] public void UpdateTag_TagDoesNotExist_NullReturned() { + // Arrange var tagDto = new TagDto { - Name = "TestTag" + Name = "TestTag1" }; var eventId = 1; var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId } }); var tagService = new TagService(mockContext.Object); + // Act var result = tagService.UpdateTag(1, tagDto, eventId); + // Assert Assert.IsNull(result); + mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } - [TestMethod] public void DeleteTag_TagExists_TagDeleted() { + // Arrange + var tags = new List { new Tag { Id = 1, Name = "TestTag" } }; var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); + mockContext.Setup(c => c.Tags).ReturnsDbSet(tags); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); // No events associated with tags + var tagService = new TagService(mockContext.Object); - - tagService.DeleteTag(1); - - mockContext.Verify(c => c.SaveChanges(), Times.Once); + + // Act + var result = tagService.DeleteTag(1); + + // Assert + Assert.IsTrue(result); + mockContext.Verify(c => c.SaveChanges(), Times.Once); // Ensure SaveChanges was called } [TestMethod] public void DeleteTag_TagDoesNotExist_NothingHappens() { + // Arrange var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); // No tags in context + mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); // No events associated with tags + var tagService = new TagService(mockContext.Object); - - tagService.DeleteTag(1); - - mockContext.Verify(c => c.SaveChanges(), Times.Never); + + // Act + var result = tagService.DeleteTag(1); + + // Assert + Assert.IsFalse(result); + mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called + } + + [TestMethod] + public void DeleteTag_TagInUse_NothingHappens() + { + // Arrange + var tag = new Tag { Id = 1, Name = "TestTag" }; + var tags = new List { tag }; + var mockContext = new Mock(); + mockContext.Setup(c => c.Tags).ReturnsDbSet(tags); + mockContext.Setup(c => c.Events).ReturnsDbSet(new List + { + new Event { Tags = new List { tag } } + }); // Tag is associated with an event + + var tagService = new TagService(mockContext.Object); + + // Act + var result = tagService.DeleteTag(1); + + // Assert + Assert.IsFalse(result); + Assert.AreEqual(1, tags.Count); // Ensure the tag was not removed + mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs deleted file mode 100644 index ccf35895..00000000 --- a/Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs +++ /dev/null @@ -1,274 +0,0 @@ -using System.Linq.Expressions; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using ReasnAPI.Models.Database; -using ReasnAPI.Models.DTOs; -using ReasnAPI.Services; - -namespace ReasnAPI.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class TestController : Controller - { - - private readonly EventService _eventService; - private readonly ImageService _imageService; - private readonly InterestService _interestService; - private readonly ParameterService _parameterService; - private readonly TagService _tagService; - - public TestController(EventService eventService, ImageService imageService, InterestService interestService, ParameterService parameterService, TagService tagService) - { - _eventService = eventService; - _imageService = imageService; - _interestService = interestService; - _parameterService = parameterService; - _tagService = tagService; - } - - [HttpGet] - [Route("event/{eventId}")] - public IActionResult GetEventById(int eventId) - { - return Ok(_eventService.GetEventById(eventId)); - } - - [HttpGet] - [Route("event/filter")] - public IActionResult GetEventsByFilter([FromBody] Expression> filter) - { - return Ok(_eventService.GetEventsByFilter(filter)); - } - - [HttpGet] - [Route("event/all")] - public IActionResult GetAllEvents() - { - return Ok(_eventService.GetAllEvents()); - } - - [HttpPost] - [Route("event/create")] - public IActionResult CreateEvent([FromBody] EventDto eventDto) - { - var @event = _eventService.CreateEvent(eventDto); - if (@event is null) - { - return StatusCode(StatusCodes.Status400BadRequest); - } - return StatusCode(StatusCodes.Status201Created); - } - - [HttpPut] - [Route("event/update/{eventId}")] - public IActionResult UpdateEvent(int eventId, [FromBody] EventDto eventDto) - { - return Ok(_eventService.UpdateEvent(eventId, eventDto)); - } - - [HttpDelete] - [Route("event/delete/{eventId}")] - public IActionResult DeleteEvent(int eventId) - { - _eventService.DeleteEvent(eventId); - return Ok(); - } - - [HttpPost] - [Route("image/create")] - public IActionResult CreateImage([FromBody] List imageDtos) - { - var image = _imageService.CreateImages(imageDtos); - if (image is null) - { - return StatusCode(StatusCodes.Status400BadRequest); - } - return StatusCode(StatusCodes.Status201Created); - } - - [HttpPut] - [Route("image/update/{imageId}")] - public IActionResult UpdateImage(int imageId, [FromBody] ImageDto imageDto) - { - return Ok(_imageService.UpdateImage(imageId, imageDto)); - } - - [HttpDelete] - [Route("image/delete/{imageId}")] - public IActionResult DeleteImage(int imageId) - { - _imageService.DeleteImage(imageId); - return Ok(); - } - - [HttpGet] - [Route("image/{imageId}")] - public IActionResult GetImageById(int imageId) - { - return Ok(_imageService.GetImageById(imageId)); - } - - [HttpGet] - [Route("image/filter")] - public IActionResult GetImagesByFilter([FromBody] Expression> filter) - { - return Ok(_imageService.GetImagesByFilter(filter)); - } - - - [HttpGet] - [Route("image/all")] - public IActionResult GetAllImages() - { - return Ok(_imageService.GetAllImages()); - } - - [HttpGet] - [Route("interest/{interestId}")] - public IActionResult GetInterestById(int interestId) - { - return Ok(_interestService.GetInterestById(interestId)); - } - - [HttpGet] - [Route("interest/all")] - public IActionResult GetAllInterests() - { - return Ok(_interestService.GetAllInterests()); - } - - [HttpGet] - [Route("interest/filter")] - public IActionResult GetInterestsByFilter([FromBody] Expression> filter) - { - return Ok(_interestService.GetInterestsByFilter(filter)); - } - - [HttpPost] - [Route("interest/create")] - public IActionResult CreateInterest([FromBody] InterestDto interestDto) - { - var interest = _interestService.CreateInterest(interestDto); - if (interest is null) - { - return StatusCode(StatusCodes.Status400BadRequest); - } - return StatusCode(StatusCodes.Status201Created); - } - - [HttpPut] - [Route("interest/update/{interestId}")] - public IActionResult UpdateInterest(int interestId, [FromBody] InterestDto interestDto) - { - return Ok(_interestService.UpdateInterest(interestId, interestDto)); - } - - [HttpDelete] - [Route("interest/delete/{interestId}")] - public IActionResult DeleteInterest(int interestId) - { - _interestService.DeleteInterest(interestId); - return Ok(); - } - - [HttpGet] - [Route("parameter/{parameterId}")] - public IActionResult GetParameterById(int parameterId) - { - return Ok(_parameterService.GetParameterById(parameterId)); - } - - [HttpGet] - [Route("parameter/all")] - public IActionResult GetAllParameters() - { - return Ok(_parameterService.GetAllParameters()); - } - - [HttpGet] - [Route("parameter/filter")] - public IActionResult GetParametersByFilter([FromBody] Expression> filter) - { - return Ok(_parameterService.GetParametersByFilter(filter)); - } - - [HttpPost] - [Route("parameter/create")] - public IActionResult CreateParameter([FromBody] ParameterDto parameterDto) - { - var parameter = _parameterService.CreateParameter(parameterDto); - if (parameter is null) - { - return StatusCode(StatusCodes.Status400BadRequest); - } - return StatusCode(StatusCodes.Status201Created); - } - - [HttpPut] - [Route("parameter/update/{parameterId}")] - public IActionResult UpdateParameter(int parameterId, [FromBody] ParameterDto parameterDto) - { - return Ok(_parameterService.UpdateParameter(parameterId, parameterDto)); - } - - [HttpDelete] - [Route("parameter/delete/{parameterId}")] - public IActionResult DeleteParameter(int parameterId) - { - _parameterService.DeleteParameter(parameterId); - return Ok(); - } - - - [HttpGet] - [Route("tag/{tagId}")] - public IActionResult GetTagById(int tagId) - { - return Ok(_tagService.GetTagById(tagId)); - } - - [HttpGet] - [Route("tag/all")] - public IActionResult GetAllTags() - { - return Ok(_tagService.GetAllTags()); - } - - [HttpPost] - [Route("tag/create")] - public IActionResult CreateTag([FromBody] TagDto tagDto) - { - var tag = _tagService.CreateTag(tagDto); - if (tag is null) - { - return StatusCode(StatusCodes.Status400BadRequest); - } - return StatusCode(StatusCodes.Status201Created); - } - - [HttpGet] - [Route("tag/filter")] - public IActionResult GetTagsByFilter([FromBody] Expression> filter) - { - return Ok(_tagService.GetTagsByFilter(filter)); - } - - [HttpPut] - [Route("tag/update/{tagId}/{eventId}")] - public IActionResult UpdateTag(int tagId, [FromBody] TagDto tagDto, int eventId) - { - return Ok(_tagService.UpdateTag(tagId, tagDto, eventId)); - } - - [HttpDelete] - [Route("tag/delete/{tagId}")] - public IActionResult DeleteTag(int tagId) - { - _tagService.DeleteTag(tagId); - return Ok(); - } - - } -} diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs index 3f6dffdd..80892807 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; +using NpgsqlTypes; namespace ReasnAPI.Models.Database; @@ -25,10 +26,10 @@ public partial class Event public DateTime UpdatedAt { get; set; } - public string Slug { get; set; } = null!; - public EventStatus Status { get; set; } + public string Slug { get; set; } = null!; + public virtual Address Address { get; set; } = null!; public virtual ICollection Comments { get; set; } = new List(); diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs index 9a2c04d1..8b09a291 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs @@ -38,6 +38,7 @@ public ReasnContext(DbContextOptions options) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + if (!optionsBuilder.IsConfigured) { optionsBuilder.UseNpgsql("name=ConnectionStrings:DefaultValue"); @@ -47,7 +48,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder - .HasPostgresEnum("common", "event_status", new[] { "Completed", "In progress", "Approved", "Waiting for approval" }) + .HasPostgresEnum("common", "event_status", new[] { "Completed", "InProgress", "Approved", "WaitingForApproval" }) .HasPostgresEnum("common", "object_type", new[] { "Event", "User" }) .HasPostgresEnum("common", "participant_status", new[] { "Interested", "Participating" }) .HasPostgresEnum("users", "role", new[] { "User", "Organizer", "Admin" }); @@ -146,7 +147,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.ImageData).HasColumnName("image_data"); entity.Property(e => e.ObjectId).HasColumnName("object_id"); - entity.Property(e => e.ObjectType).HasColumnName("object_type"); + entity.Property(i => i.ObjectType).HasColumnName("object_type"); }); modelBuilder.Entity(entity => @@ -252,7 +253,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.Surname).HasColumnName("surname"); entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); entity.Property(e => e.Username).HasColumnName("username"); - entity.Property(e => e.Role).HasColumnName("role"); + entity.Property(u => u.Role).HasColumnName("role"); entity.HasOne(d => d.Address).WithMany(p => p.Users) .HasForeignKey(d => d.AddressId) diff --git a/Server/ReasnAPI/ReasnAPI/Program.cs b/Server/ReasnAPI/ReasnAPI/Program.cs index 3ba93ee0..b93353e9 100644 --- a/Server/ReasnAPI/ReasnAPI/Program.cs +++ b/Server/ReasnAPI/ReasnAPI/Program.cs @@ -7,6 +7,9 @@ using ReasnAPI.Models.DTOs; using ReasnAPI.Models.Database; using ReasnAPI.Services; +using Npgsql; +using ReasnAPI.Models.Enums; +using Microsoft.Extensions.Configuration; var builder = WebApplication.CreateSlimBuilder(args); @@ -30,7 +33,6 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); - builder.Services.AddControllers(); builder.Services.AddSwaggerGen(options => diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 479c3079..bd3a8e67 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -4,6 +4,7 @@ using System.Linq.Expressions; using System.Linq; using System.Transactions; +using ReasnAPI.Models.Enums; namespace ReasnAPI.Services; public class EventService(ReasnContext context) @@ -13,7 +14,7 @@ public class EventService(ReasnContext context) using (var scope = new TransactionScope()) { eventDto.Slug = CreateSlug(eventDto); - var nowTime = DateTime.Now; + var nowTime = DateTime.UtcNow; var newEvent = new Event { Name = eventDto.Name, @@ -25,7 +26,7 @@ public class EventService(ReasnContext context) CreatedAt = nowTime, UpdatedAt = nowTime, Slug = eventDto.Slug, - Status = eventDto.Status, + Status = eventDto.Status }; context.Events.Add(newEvent); @@ -61,6 +62,7 @@ public class EventService(ReasnContext context) } context.SaveChanges(); + scope.Complete(); } return eventDto; @@ -168,7 +170,7 @@ public class EventService(ReasnContext context) context.SaveChanges(); } - + scope.Complete(); } return eventDto; @@ -187,6 +189,7 @@ public bool DeleteEvent(int eventId) context.Events.Remove(eventToDelete); context.SaveChanges(); + scope.Complete(); } return true; diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 94d14785..2116ffc4 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -38,8 +38,10 @@ public class TagService (ReasnContext context) var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); - var eventTags = eventsWithTags.Where(e => e.Tags.Any(t => t.Id == tagId) && e.Id != eventId).ToList(); - if (eventTags.Any()) // if tag is associated with more than one event + var eventTags = eventsWithTags.Where(e => e.Tags.Any(t => t.Id == tagId)).ToList(); + var eventToUpdate = eventsWithTags.FirstOrDefault(e => e.Id == eventId); + + if (eventTags.Count > 1 || (eventTags.Count == 1 && eventTags[0].Id != eventId)) { // Create new tag, associate it with the event, and remove the old association var newTag = new Tag @@ -49,14 +51,14 @@ public class TagService (ReasnContext context) context.Tags.Add(newTag); context.SaveChanges(); - var eventToUpdate = eventsWithTags.FirstOrDefault(e => e.Id == eventId); if (eventToUpdate != null) { eventToUpdate.Tags.Remove(tag); eventToUpdate.Tags.Add(newTag); + context.Events.Update(eventToUpdate); } } - else if (eventTags.Count == 1 && eventTags[0].Id == eventId) // if tag is associated only with the same event + else if (eventTags.Count == 1 && eventTags[0].Id == eventId) { tag.Name = tagDto.Name; context.Tags.Update(tag); From 242842502d69bdcb93f8c442051ed1f6798d24b7 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sun, 9 Jun 2024 15:19:19 +0200 Subject: [PATCH 19/65] created mappers and small fixes --- .../ReasnAPI/Models/DTOs/InterestDto.cs | 6 +- Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj | 3 + .../ReasnAPI/Services/EventService.cs | 165 +++++++----------- .../ReasnAPI/Services/ImageService.cs | 55 +++--- .../ReasnAPI/Services/InterestService.cs | 16 +- .../ReasnAPI/Services/ParameterService.cs | 43 +++-- 6 files changed, 117 insertions(+), 171 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs b/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs index 03872287..b4225204 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs @@ -1,7 +1,9 @@ using System; -namespace ReasnAPI.Models.DTOs { - public class InterestDto { +namespace ReasnAPI.Models.DTOs +{ + public class InterestDto + { public string Name { get; set; } = null!; } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj index 25ee8a40..23499ae8 100644 --- a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj +++ b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj @@ -21,7 +21,10 @@ + + + diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index bd3a8e67..b3c816a1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -15,30 +15,17 @@ public class EventService(ReasnContext context) { eventDto.Slug = CreateSlug(eventDto); var nowTime = DateTime.UtcNow; - var newEvent = new Event - { - Name = eventDto.Name, - AddressId = eventDto.AddressId, - Description = eventDto.Description, - OrganizerId = eventDto.OrganizerId, - StartAt = eventDto.StartAt, - EndAt = eventDto.EndAt, - CreatedAt = nowTime, - UpdatedAt = nowTime, - Slug = eventDto.Slug, - Status = eventDto.Status - }; - + var newEvent = MapEventFromDto(eventDto); + newEvent.CreatedAt = nowTime; + newEvent.UpdatedAt = nowTime; + newEvent.Slug = eventDto.Slug; + context.Events.Add(newEvent); context.SaveChanges(); var eventId = newEvent.Id; - var addedEvent = context.Events.Include(e => e.Tags).Include(e => e.Parameters) - .FirstOrDefault(e => e.Id == eventId); - - if (addedEvent == null) return null; - if (eventDto.Tags is not null && eventDto.Tags.Count > 0) + if (eventDto.Tags is not null && eventDto.Tags.Any()) { var newTags = eventDto.Tags .Where(t => !context.Tags.Any(x => x.Name == t.Name)) @@ -46,10 +33,10 @@ public class EventService(ReasnContext context) .ToList(); context.Tags.AddRange(newTags); - addedEvent.Tags = newTags; + newEvent.Tags = newTags; } - if (eventDto.Parameters is not null && eventDto.Parameters.Count > 0) + if (eventDto.Parameters is not null && eventDto.Parameters.Any()) { var newParameters = eventDto.Parameters .Where(p => !context.Parameters.Any(x => x.Key == p.Key && x.Value == p.Value)) @@ -58,7 +45,7 @@ public class EventService(ReasnContext context) context.Parameters.AddRange(newParameters); - addedEvent.Parameters = newParameters; + newEvent.Parameters = newParameters; } context.SaveChanges(); @@ -90,15 +77,6 @@ public class EventService(ReasnContext context) context.Events.Update(eventToUpdate); context.SaveChanges(); - var existingEvent = context.Events - .Include(e => e.Tags) - .FirstOrDefault(e => e.Id == eventId); - - if (existingEvent is null) - { - return null; - } - if (eventDto.Tags is not null) { @@ -108,13 +86,13 @@ public class EventService(ReasnContext context) Name = tagDto.Name, }).ToList(); - var tagsToRemove = existingEvent.Tags + var tagsToRemove = eventToUpdate.Tags .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) .ToList(); foreach (var tagToRemove in tagsToRemove) { - existingEvent.Tags.Remove(tagToRemove); + eventToUpdate.Tags.Remove(tagToRemove); } var existingTagsInDb = context.Tags.ToList(); @@ -130,7 +108,7 @@ public class EventService(ReasnContext context) newTag) .ToList(); - existingEvent.Tags = updatedTags; + eventToUpdate.Tags = updatedTags; context.SaveChanges(); } @@ -144,13 +122,13 @@ public class EventService(ReasnContext context) Value = paramDto.Value }).ToList(); - var paramsToRemove = existingEvent.Parameters + var paramsToRemove = eventToUpdate.Parameters .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key)) .ToList(); foreach (var paramToRemove in paramsToRemove) { - existingEvent.Parameters.Remove(paramToRemove); + eventToUpdate.Parameters.Remove(paramToRemove); } var existingParamsInDb = context.Parameters.ToList(); @@ -166,7 +144,7 @@ public class EventService(ReasnContext context) newParam) .ToList(); - existingEvent.Parameters = updatedParams; + eventToUpdate.Parameters = updatedParams; context.SaveChanges(); } @@ -203,29 +181,7 @@ public bool DeleteEvent(int eventId) return null; } - var tags = eventToReturn.Tags - .Select(t => new TagDto { Name = t.Name }) - .ToList(); - - var parameters = eventToReturn.Parameters - .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) - .ToList(); - - var eventDto = new EventDto - { - Name = eventToReturn.Name, - AddressId = eventToReturn.AddressId, - Description = eventToReturn.Description, - OrganizerId = eventToReturn.OrganizerId, - StartAt = eventToReturn.StartAt, - EndAt = eventToReturn.EndAt, - CreatedAt = eventToReturn.CreatedAt, - UpdatedAt = eventToReturn.UpdatedAt, - Slug = eventToReturn.Slug, - Status = eventToReturn.Status, - Tags = tags, - Parameters = parameters - }; + var eventDto = MapEventDtoFromEvent(eventToReturn); return eventDto; } @@ -236,29 +192,7 @@ public IEnumerable GetEventsByFilter(Expression> fil var eventDtos = new List(); foreach (var eventToReturn in events) { - var tags = eventToReturn.Tags - .Select(t => new TagDto { Name = t.Name }) - .ToList(); - - var parameters = eventToReturn.Parameters - .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) - .ToList(); - - var eventDto = new EventDto - { - Name = eventToReturn.Name, - AddressId = eventToReturn.AddressId, - Description = eventToReturn.Description, - OrganizerId = eventToReturn.OrganizerId, - StartAt = eventToReturn.StartAt, - EndAt = eventToReturn.EndAt, - CreatedAt = eventToReturn.CreatedAt, - UpdatedAt = eventToReturn.UpdatedAt, - Slug = eventToReturn.Slug, - Status = eventToReturn.Status, - Tags = tags, - Parameters = parameters - }; + var eventDto = MapEventDtoFromEvent(eventToReturn); eventDtos.Add(eventDto); } @@ -273,29 +207,7 @@ public IEnumerable GetAllEvents() var eventDtos = new List(); foreach (var eventToReturn in events) { - var tags = eventToReturn.Tags - .Select(t => new TagDto { Name = t.Name }) - .ToList(); - - var parameters = eventToReturn.Parameters - .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) - .ToList(); - - var eventDto = new EventDto - { - Name = eventToReturn.Name, - AddressId = eventToReturn.AddressId, - Description = eventToReturn.Description, - OrganizerId = eventToReturn.OrganizerId, - StartAt = eventToReturn.StartAt, - EndAt = eventToReturn.EndAt, - CreatedAt = eventToReturn.CreatedAt, - UpdatedAt = eventToReturn.UpdatedAt, - Slug = eventToReturn.Slug, - Status = eventToReturn.Status, - Tags = tags, - Parameters = parameters - }; + var eventDto = MapEventDtoFromEvent(eventToReturn); eventDtos.Add(eventDto); } @@ -309,4 +221,45 @@ private string CreateSlug(EventDto eventDto) return slug; } + private Event MapEventFromDto(EventDto eventDto) + { + return new Event + { + Name = eventDto.Name, + AddressId = eventDto.AddressId, + Description = eventDto.Description, + OrganizerId = eventDto.OrganizerId, + StartAt = eventDto.StartAt, + EndAt = eventDto.EndAt, + Status = eventDto.Status + }; + } + + private EventDto MapEventDtoFromEvent(Event eventToMap) + { + var tags = eventToMap.Tags + .Select(t => new TagDto { Name = t.Name }) + .ToList(); + + var parameters = eventToMap.Parameters + .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) + .ToList(); + + return new EventDto + { + Name = eventToMap.Name, + AddressId = eventToMap.AddressId, + Description = eventToMap.Description, + OrganizerId = eventToMap.OrganizerId, + StartAt = eventToMap.StartAt, + EndAt = eventToMap.EndAt, + CreatedAt = eventToMap.CreatedAt, + UpdatedAt = eventToMap.UpdatedAt, + Slug = eventToMap.Slug, + Status = eventToMap.Status, + Tags = tags, + Parameters = parameters + }; + } + } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 1ee5389f..60f4cb2b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -20,13 +20,8 @@ public List CreateImages(List imageDtos) continue; } - var newImage = new Image - { - ImageData = imageDto.ImageData, - ObjectId = imageDto.ObjectId, - ObjectType = imageDto.ObjectType - }; - + var newImage = MapImageFromImageDto(imageDto); + newImages.Add(newImage); } @@ -89,13 +84,8 @@ public bool DeleteImage(int id) return null; } - var imageDto = new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectType = image.ObjectType - }; - + var imageDto = MapImageDtoFromImage(image); + return imageDto; } @@ -103,12 +93,7 @@ public IEnumerable GetAllImages() { var images = context.Images.ToList(); - var imageDtos = images.Select(image => new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectType = image.ObjectType - }).ToList(); + var imageDtos = images.Select(image => MapImageDtoFromImage(image)).AsEnumerable(); return imageDtos; } @@ -117,12 +102,7 @@ public IEnumerable GetImagesByFilter(Expression> fil { var images = context.Images.Where(filter).ToList(); - var imageDtos = images.Select(image => new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectType = image.ObjectType - }).AsEnumerable(); + var imageDtos = images.Select(image => MapImageDtoFromImage(image)).AsEnumerable(); return imageDtos; } @@ -133,14 +113,29 @@ public IEnumerable GetImagesByEventIdAndType(int eventId) .Where(image => image.ObjectType == ObjectType.Event && image.ObjectId == eventId) .ToList(); - var imageDtos = images.Select(image => new ImageDto + var imageDtos = images.Select(image => MapImageDtoFromImage(image)).AsEnumerable(); + + return imageDtos; + } + + private ImageDto MapImageDtoFromImage(Image image) + { + return new ImageDto { ImageData = image.ImageData, ObjectId = image.ObjectId, ObjectType = image.ObjectType - }).AsEnumerable(); - - return imageDtos; + }; } + + private Image MapImageFromImageDto(ImageDto imageDto) + { + return new Image + { + ImageData = imageDto.ImageData, + ObjectId = imageDto.ObjectId, + ObjectType = imageDto.ObjectType + }; + } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index cdfb4579..0182d277 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -56,21 +56,7 @@ public bool DeleteInterest(int id) 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; - //} + } public InterestDto? GetInterestById(int interestId) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 0fdca0c7..4a4460b0 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -14,11 +14,7 @@ public class ParameterService(ReasnContext context) return null; } - var newParameter = new Parameter - { - Key = parameterDto.Key, - Value = parameterDto.Value - }; + var newParameter = MapParameterFromParameterDto(parameterDto); context.Parameters.Add(newParameter); context.SaveChanges(); return parameterDto; @@ -32,15 +28,11 @@ public class ParameterService(ReasnContext context) var parameterCheck = parameters.FirstOrDefault(r => r.Parameters.Any(p => p.Id == parameterId)); - if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated - { - return null; - } - - if (parameter is null) + if (parameterCheck is not null && parameter is null) // if parameter is associated with an event, it cannot be updated { return null; } + parameter.Key = parameterDto.Key; parameter.Value = parameterDto.Value; context.Parameters.Update(parameter); @@ -82,11 +74,7 @@ public bool DeleteParameter(int parameterId) return null; } - var parameterDto = new ParameterDto - { - Key = parameter.Key, - Value = parameter.Value - }; + var parameterDto = MapParameterDtoFromParameter(parameter); return parameterDto; } @@ -95,15 +83,34 @@ public IEnumerable GetAllParameters() { var parameters = context.Parameters.ToList(); - return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }).AsEnumerable(); + return parameters.Select(parameter => MapParameterDtoFromParameter(parameter)) + .AsEnumerable(); } public IEnumerable GetParametersByFilter(Expression> filter) { var parameters = context.Parameters.Where(filter).ToList(); - return parameters.Select(parameter => new ParameterDto { Key = parameter.Key, Value = parameter.Value }) + return parameters.Select(parameter => MapParameterDtoFromParameter(parameter)) .AsEnumerable(); } + private Parameter MapParameterFromParameterDto(ParameterDto parameterDto) + { + return new Parameter + { + Key = parameterDto.Key, + Value = parameterDto.Value + }; + } + + private ParameterDto MapParameterDtoFromParameter(Parameter parameter) + { + return new ParameterDto + { + Key = parameter.Key, + Value = parameter.Value + }; + } + } \ No newline at end of file From 095dd7de72de60db40d3812820043ff2e381cbf8 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sun, 9 Jun 2024 17:57:58 +0200 Subject: [PATCH 20/65] finished mappers --- .../ReasnAPI/Services/InterestService.cs | 31 +++++++++------- .../ReasnAPI/Services/ParameterService.cs | 2 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 35 +++++++++++-------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 0182d277..c197a50f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -14,10 +14,7 @@ public class InterestService(ReasnContext context) return null; } - var newInterest = new Interest - { - Name = interestDto.Name - }; + var newInterest = MapInterestFromInterestDto(interestDto); context.Interests.Add(newInterest); context.SaveChanges(); @@ -66,10 +63,7 @@ public bool DeleteInterest(int id) return null; } - var interestDto = new InterestDto - { - Name = interest.Name - }; + var interestDto = MapInterestDtoFromInterest(interest); return interestDto; } @@ -78,19 +72,32 @@ public IEnumerable GetAllInterests() { var interests = context.Interests.ToList(); - return interests.Select(interest => new InterestDto { Name = interest.Name }).AsEnumerable(); + return interests.Select(interest => MapInterestDtoFromInterest(interest)).AsEnumerable(); } public IEnumerable GetInterestsByFilter(Expression> filter) { var interests = context.Interests.Where(filter).ToList(); - var interestDtos = interests.Select(interest => new InterestDto + var interestDtos = interests.Select(interest => MapInterestDtoFromInterest(interest)).AsEnumerable(); + + return interestDtos; + } + + private InterestDto MapInterestDtoFromInterest(Interest interest) + { + return new InterestDto { Name = interest.Name - }).AsEnumerable(); + }; + } - return interestDtos; + private Interest MapInterestFromInterestDto(InterestDto interestDto) + { + return new Interest + { + Name = interestDto.Name + }; } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 4a4460b0..33028ee1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -28,7 +28,7 @@ public class ParameterService(ReasnContext context) var parameterCheck = parameters.FirstOrDefault(r => r.Parameters.Any(p => p.Id == parameterId)); - if (parameterCheck is not null && parameter is null) // if parameter is associated with an event, it cannot be updated + if (parameterCheck is not null || parameter is null) // if parameter is associated with an event, it cannot be updated { return null; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 2116ffc4..188b192e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -16,10 +16,7 @@ public class TagService (ReasnContext context) return null; } - var newTag = new Tag - { - Name = tagDto.Name - }; + var newTag = MapTagFromTagDto(tagDto); context.Tags.Add(newTag); context.SaveChanges(); return tagDto; @@ -44,10 +41,7 @@ public class TagService (ReasnContext context) if (eventTags.Count > 1 || (eventTags.Count == 1 && eventTags[0].Id != eventId)) { // Create new tag, associate it with the event, and remove the old association - var newTag = new Tag - { - Name = tagDto.Name - }; + var newTag = MapTagFromTagDto(tagDto); context.Tags.Add(newTag); context.SaveChanges(); @@ -106,22 +100,35 @@ public bool DeleteTag(int tagId) return null; } - return new TagDto - { - Name = tag.Name - }; + return MapTagDtoFromTag(tag); } public IEnumerable GetAllTags() { var tags = context.Tags.ToList(); - return tags.Select(tag => new TagDto { Name = tag.Name }).AsEnumerable(); + return tags.Select(tag => MapTagDtoFromTag(tag)).AsEnumerable(); } public IEnumerable GetTagsByFilter(Expression> filter) { var tags = context.Tags.Where(filter).ToList(); - return tags.Select(tag => new TagDto { Name = tag.Name }).AsEnumerable(); + return tags.Select(tag => MapTagDtoFromTag(tag)).AsEnumerable(); + } + + private TagDto MapTagDtoFromTag(Tag tag) + { + return new TagDto + { + Name = tag.Name + }; + } + + private Tag MapTagFromTagDto(TagDto tagDto) + { + return new Tag + { + Name = tagDto.Name + }; } } \ No newline at end of file From 028d04366fb7667be12b2144d6478e1d3fb7052a Mon Sep 17 00:00:00 2001 From: Maciej Koperdowski Date: Sun, 9 Jun 2024 19:33:23 +0200 Subject: [PATCH 21/65] Added exceptions in services --- .../Services/EventServicesTest.cs | 9 ++--- .../Services/ImageServiceTests.cs | 12 +++---- .../Services/InterestServiceTests.cs | 27 ++++----------- .../Services/ParameterServiceTests.cs | 28 ++++------------ .../Services/TagServiceTests.cs | 33 ++++--------------- .../ReasnAPI/Services/EventService.cs | 18 +++++----- .../Services/Exceptions/NotFoundException.cs | 10 ++++++ .../Exceptions/ObjectExistsException.cs | 9 +++++ .../Exceptions/ObjectInUseException.cs | 10 ++++++ .../ReasnAPI/Services/ImageService.cs | 18 +++++----- .../ReasnAPI/Services/InterestService.cs | 28 ++++++++-------- .../ReasnAPI/Services/ParameterService.cs | 27 ++++++++------- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 23 +++++++------ 13 files changed, 116 insertions(+), 136 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs create mode 100644 Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs create mode 100644 Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 0b7b17a6..9a11c313 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -9,6 +9,7 @@ using Moq; using Moq.EntityFrameworkCore; using ReasnAPI.Models.Enums; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Tests.Services @@ -217,9 +218,7 @@ public void UpdateEvent_EventDoesNotExist_NullReturned() var eventService = new EventService(mockContext.Object); - var result = eventService.UpdateEvent(1, eventDto); - - Assert.IsNull(result); + Assert.ThrowsException(() => eventService.UpdateEvent(1, eventDto)); } [TestMethod] @@ -325,9 +324,7 @@ public void GetEventById_EventDoesNotExist_NullReturned() var eventService = new EventService(mockContext.Object); - var result = eventService.GetEventById(1); - - Assert.IsNull(result); + Assert.ThrowsException(() => eventService.GetEventById(1)); } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index f3f3308b..951acee7 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -10,6 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Tests.Services @@ -115,8 +116,7 @@ public void GetImageById_ImageDoesNotExist_NullReturned() var imageService = new ImageService(mockContext.Object); - var result = imageService.GetImageById(1); - Assert.IsNull(result); + Assert.ThrowsException(() => imageService.GetImageById(1)); } [TestMethod] @@ -153,9 +153,7 @@ public void UpdateImage_ImageDoesNotExist_NullReturned() var imageService = new ImageService(mockContext.Object); - var result = imageService.UpdateImage(1, imageDto); - - Assert.IsNull(result); + Assert.ThrowsException(() => imageService.UpdateImage(1, imageDto)); } [TestMethod] @@ -184,9 +182,7 @@ public void DeleteImage_ImageDoesNotExist_NothingDeleted() var imageService = new ImageService(mockContext.Object); - imageService.DeleteImage(1); - - mockContext.Verify(c => c.SaveChanges(), Times.Never); + Assert.ThrowsException(() => imageService.DeleteImage(1)); } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index 62b38786..2f17fdb9 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -10,6 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Tests.Services { @@ -47,9 +48,7 @@ public void CreateInterest_InterestExists_InterestNotCreated() var interestService = new InterestService(mockContext.Object); - var result = interestService.CreateInterest(interestDto); - - Assert.IsNull(result); + Assert.ThrowsException(() => interestService.CreateInterest(interestDto)); } [TestMethod] @@ -86,9 +85,7 @@ public void GetInterestById_InterestDoesNotExist_NullReturned() var interestService = new InterestService(mockContext.Object); - var result = interestService.GetInterestById(1); - - Assert.IsNull(result); + Assert.ThrowsException(() => interestService.GetInterestById(1)); } //[TestMethod] @@ -135,9 +132,7 @@ public void UpdateInterest_InterestDoesNotExist_NullReturned() var interestService = new InterestService(mockContext.Object); - var result = interestService.UpdateInterest(1, interestDto); - - Assert.IsNull(result); + Assert.ThrowsException(() => interestService.UpdateInterest(1, interestDto)); } [TestMethod] @@ -152,9 +147,8 @@ public void DeleteInterest_InterestExists_InterestDeleted() var interestService = new InterestService(mockContext.Object); - var result = interestService.DeleteInterest(1); + interestService.DeleteInterest(1); - Assert.IsTrue(result); mockContext.Verify(c => c.Interests.Remove(It.Is(i => i.Id == 1)), Times.Once); mockContext.Verify(c => c.SaveChanges(), Times.Once); } @@ -168,11 +162,7 @@ public void DeleteInterest_InterestDoesNotExist_NothingDeleted() var interestService = new InterestService(mockContext.Object); - var result = interestService.DeleteInterest(1); - - Assert.IsFalse(result); - mockContext.Verify(c => c.Interests.Remove(It.IsAny()), Times.Never); - mockContext.Verify(c => c.SaveChanges(), Times.Never); + Assert.ThrowsException(() => interestService.DeleteInterest(1)); } [TestMethod] @@ -188,11 +178,8 @@ public void DeleteInterest_InterestHasUserInterests_NothingDeleted() new UserInterest { InterestId = 1, UserId = 1 } }); - var interestService = new InterestService(mockContext.Object); - - var result = interestService.DeleteInterest(1); + Assert.ThrowsException(() => new InterestService(mockContext.Object).DeleteInterest(1)); - Assert.IsFalse(result); mockContext.Verify(c => c.Interests.Remove(It.IsAny()), Times.Never); mockContext.Verify(c => c.SaveChanges(), Times.Never); } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index 5b3a065e..642d6bf7 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -10,6 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Tests.Services { @@ -49,9 +50,7 @@ public void CreateParameter_ParameterExists_ParameterNotCreated() var parameterService = new ParameterService(mockContext.Object); - var result = parameterService.CreateParameter(parameterDto); - - Assert.IsNull(result); + Assert.ThrowsException(() => parameterService.CreateParameter(parameterDto)); } [TestMethod] @@ -85,7 +84,6 @@ public void UpdateParameter_ParameterExists_ParameterUpdated() [TestMethod] public void UpdateParameter_ParameterDoesNotExist_NullReturned() { - // Arrange var parameterDto = new ParameterDto { Key = "TestKey", @@ -98,18 +96,13 @@ public void UpdateParameter_ParameterDoesNotExist_NullReturned() var parameterService = new ParameterService(mockContext.Object); - // Act - var result = parameterService.UpdateParameter(1, parameterDto); - - // Assert - Assert.IsNull(result); + Assert.ThrowsException(() => parameterService.UpdateParameter(1, parameterDto)); mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } [TestMethod] public void UpdateParameter_ParameterInUse_NullReturned() { - // Arrange var parameterDto = new ParameterDto { Key = "UpdatedKey", @@ -126,11 +119,7 @@ public void UpdateParameter_ParameterInUse_NullReturned() var parameterService = new ParameterService(mockContext.Object); - // Act - var result = parameterService.UpdateParameter(1, parameterDto); - - // Assert - Assert.IsNull(result); + Assert.ThrowsException(() => parameterService.UpdateParameter(1, parameterDto)); mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } @@ -142,9 +131,7 @@ public void GetParameterById_ParameterDoesNotExist_NullReturned() var parameterService = new ParameterService(mockContext.Object); - var result = parameterService.GetParameterById(1); - - Assert.IsNull(result); + Assert.ThrowsException(() => parameterService.GetParameterById(1)); } //[TestMethod] @@ -177,10 +164,9 @@ public void DeleteParameter_ParameterExists_ParameterDeleted() var parameterService = new ParameterService(mockContext.Object); // Act - var result = parameterService.DeleteParameter(1); + parameterService.DeleteParameter(1); // Assert - Assert.IsTrue(result); mockContext.Verify(c => c.SaveChanges(), Times.Once); } @@ -191,7 +177,7 @@ public void DeleteParameter_ParameterDoesNotExist_NothingDeleted() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); - parameterService.DeleteParameter(1); + Assert.ThrowsException(() => parameterService.DeleteParameter(1)); mockContext.Verify(c => c.SaveChanges(), Times.Never); } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index dd6eed32..54aecb08 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -10,6 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Tests.Services { @@ -47,9 +48,7 @@ public void CreateTag_TagExists_TagNotCreated() var tagService = new TagService(mockContext.Object); - var result = tagService.CreateTag(tagDto); - - Assert.IsNull(result); + Assert.ThrowsException(() => tagService.CreateTag(tagDto)); } [TestMethod] @@ -99,9 +98,7 @@ public void GetTagById_TagDoesNotExist_NullReturned() var tagService = new TagService(mockContext.Object); - var result = tagService.GetTagById(1); - - Assert.IsNull(result); + Assert.ThrowsException(() => tagService.GetTagById(1)); } [TestMethod] @@ -134,7 +131,6 @@ public void UpdateTag_TagExists_TagUpdated() [TestMethod] public void UpdateTag_TagDoesNotExist_NullReturned() { - // Arrange var tagDto = new TagDto { Name = "TestTag1" @@ -146,11 +142,7 @@ public void UpdateTag_TagDoesNotExist_NullReturned() mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId } }); var tagService = new TagService(mockContext.Object); - // Act - var result = tagService.UpdateTag(1, tagDto, eventId); - - // Assert - Assert.IsNull(result); + Assert.ThrowsException(() => tagService.UpdateTag(1, tagDto, eventId)); mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } @@ -166,35 +158,28 @@ public void DeleteTag_TagExists_TagDeleted() var tagService = new TagService(mockContext.Object); // Act - var result = tagService.DeleteTag(1); + tagService.DeleteTag(1); // Assert - Assert.IsTrue(result); mockContext.Verify(c => c.SaveChanges(), Times.Once); // Ensure SaveChanges was called } [TestMethod] public void DeleteTag_TagDoesNotExist_NothingHappens() { - // Arrange var mockContext = new Mock(); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); // No tags in context mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); // No events associated with tags var tagService = new TagService(mockContext.Object); - // Act - var result = tagService.DeleteTag(1); - - // Assert - Assert.IsFalse(result); + Assert.ThrowsException(() => tagService.DeleteTag(1)); mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } [TestMethod] public void DeleteTag_TagInUse_NothingHappens() { - // Arrange var tag = new Tag { Id = 1, Name = "TestTag" }; var tags = new List { tag }; var mockContext = new Mock(); @@ -206,11 +191,7 @@ public void DeleteTag_TagInUse_NothingHappens() var tagService = new TagService(mockContext.Object); - // Act - var result = tagService.DeleteTag(1); - - // Assert - Assert.IsFalse(result); + Assert.ThrowsException(() => tagService.DeleteTag(1)); Assert.AreEqual(1, tags.Count); // Ensure the tag was not removed mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index b3c816a1..67a74c52 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -4,12 +4,13 @@ using System.Linq.Expressions; using System.Linq; using System.Transactions; -using ReasnAPI.Models.Enums; +using ReasnAPI.Models.Enums; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Services; public class EventService(ReasnContext context) { - public EventDto? CreateEvent(EventDto eventDto) + public EventDto CreateEvent(EventDto eventDto) { using (var scope = new TransactionScope()) { @@ -55,14 +56,15 @@ public class EventService(ReasnContext context) return eventDto; } - public EventDto? UpdateEvent(int eventId, EventDto eventDto) + public EventDto UpdateEvent(int eventId, EventDto eventDto) { using (var scope = new TransactionScope()) { var eventToUpdate = context.Events.FirstOrDefault(r => r.Id == eventId); + if (eventToUpdate is null) { - return null; + throw new NotFoundException("Event not found"); } eventToUpdate.Name = eventDto.Name; @@ -154,7 +156,7 @@ public class EventService(ReasnContext context) return eventDto; } - public bool DeleteEvent(int eventId) + public void DeleteEvent(int eventId) { using (var scope = new TransactionScope()) { @@ -162,15 +164,13 @@ public bool DeleteEvent(int eventId) if (eventToDelete is null) { - return false; + throw new NotFoundException("Event not found"); } context.Events.Remove(eventToDelete); context.SaveChanges(); scope.Complete(); } - - return true; } public EventDto? GetEventById(int eventId) @@ -178,7 +178,7 @@ public bool DeleteEvent(int eventId) var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Id == eventId); if (eventToReturn is null) { - return null; + throw new NotFoundException("Event not found"); } var eventDto = MapEventDtoFromEvent(eventToReturn); diff --git a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs new file mode 100644 index 00000000..1eadad1e --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs @@ -0,0 +1,10 @@ +namespace ReasnAPI.Services.Exceptions; + +public class NotFoundException : Exception +{ + public NotFoundException() { } + + public NotFoundException(string message) : base(message) { } + + public NotFoundException(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs new file mode 100644 index 00000000..f6dfa0ac --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs @@ -0,0 +1,9 @@ +namespace ReasnAPI.Services.Exceptions; +public class ObjectExistsException : Exception +{ + public ObjectExistsException() : base() { } + + public ObjectExistsException(string message) : base(message) { } + + public ObjectExistsException(string message, Exception innerException) : base(message, innerException) { } +} diff --git a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs new file mode 100644 index 00000000..182a65dd --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs @@ -0,0 +1,10 @@ +namespace ReasnAPI.Services.Exceptions; + +public class ObjectInUseException : Exception +{ + public ObjectInUseException() { } + + public ObjectInUseException(string message) : base(message) { } + + public ObjectInUseException(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 60f4cb2b..2607dbdf 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -3,7 +3,8 @@ using System.Linq.Expressions; using ReasnAPI.Models.Enums; using static System.Net.Mime.MediaTypeNames; -using Image = ReasnAPI.Models.Database.Image; +using Image = ReasnAPI.Models.Database.Image; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Services; public class ImageService(ReasnContext context) @@ -46,12 +47,12 @@ public List CreateImages(List imageDtos) return imageDtos; } - public ImageDto? UpdateImage(int imageId, ImageDto imageDto) + public ImageDto UpdateImage(int imageId, ImageDto imageDto) { var image = context.Images.FirstOrDefault(r => r.Id == imageId); if (image is null) { - return null; + throw new NotFoundException("Image not found"); } image.ObjectId = imageDto.ObjectId; @@ -63,25 +64,24 @@ public List CreateImages(List imageDtos) return imageDto; } - public bool DeleteImage(int id) + public void DeleteImage(int id) { var image = context.Images.FirstOrDefault(r => r.Id == id); if (image is null) { - return false; + throw new NotFoundException("Image not found"); } + context.Images.Remove(image); context.SaveChanges(); - - return true; } - public ImageDto? GetImageById(int id) + public ImageDto GetImageById(int id) { var image = context.Images.Find(id); if (image is null) { - return null; + throw new NotFoundException("Image not found"); } var imageDto = MapImageDtoFromImage(image); diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index c197a50f..a8281072 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -1,17 +1,18 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; +using ReasnAPI.Services.Exceptions; using System.Linq.Expressions; namespace ReasnAPI.Services; public class InterestService(ReasnContext context) { - public InterestDto? CreateInterest(InterestDto interestDto) + public InterestDto CreateInterest(InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Name == interestDto.Name); if (interest is not null) { - return null; + throw new ObjectExistsException("Interest already exists"); } var newInterest = MapInterestFromInterestDto(interestDto); @@ -21,12 +22,12 @@ public class InterestService(ReasnContext context) return interestDto; } - public InterestDto? UpdateInterest(int interestId, InterestDto interestDto) + public InterestDto UpdateInterest(int interestId, InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); if (interest is null) { - return null; + throw new NotFoundException("Interest not found"); } interest.Name = interestDto.Name; @@ -36,31 +37,30 @@ public class InterestService(ReasnContext context) return interestDto; } - public bool DeleteInterest(int id) + public void DeleteInterest(int id) { var interest = context.Interests.FirstOrDefault(r => r.Id == id); - var eventInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id); - if (eventInterest is not null) + if (interest is null) { - return false; + throw new NotFoundException("Interest not found"); } - if (interest is null) + var eventInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id); + if (eventInterest is not null) { - return false; + throw new ObjectInUseException("Interest is in use"); } + context.Interests.Remove(interest); context.SaveChanges(); - - return true; } - public InterestDto? GetInterestById(int interestId) + public InterestDto GetInterestById(int interestId) { var interest = context.Interests.Find(interestId); if (interest is null) { - return null; + throw new NotFoundException("Interest not found"); } var interestDto = MapInterestDtoFromInterest(interest); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 33028ee1..a60e0e8b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -2,16 +2,17 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Services; public class ParameterService(ReasnContext context) { - public ParameterDto? CreateParameter(ParameterDto parameterDto) + public ParameterDto CreateParameter(ParameterDto parameterDto) { var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key && r.Value == parameterDto.Value); if (parameter is not null) { - return null; + throw new ObjectExistsException("Parameter already exists"); } var newParameter = MapParameterFromParameterDto(parameterDto); @@ -20,17 +21,22 @@ public class ParameterService(ReasnContext context) return parameterDto; } - public ParameterDto? UpdateParameter(int parameterId, ParameterDto parameterDto) + public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + if (parameter is null) + { + throw new NotFoundException("Parameter not found"); + } + var parameters = context.Events.Include(p => p.Parameters); var parameterCheck = parameters.FirstOrDefault(r => r.Parameters.Any(p => p.Id == parameterId)); - if (parameterCheck is not null || parameter is null) // if parameter is associated with an event, it cannot be updated + if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated { - return null; + throw new ObjectInUseException("Parameter is associated with an event"); } parameter.Key = parameterDto.Key; @@ -40,13 +46,13 @@ public class ParameterService(ReasnContext context) return parameterDto; } - public bool DeleteParameter(int parameterId) + public void DeleteParameter(int parameterId) { var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); if (parameter == null) { - return false; + throw new NotFoundException("Parameter not found"); } var eventsWithParameters = context.Events @@ -58,20 +64,19 @@ public bool DeleteParameter(int parameterId) if (parameterCheck) { - return false; + throw new ObjectInUseException("Parameter is associated with an event"); } context.Parameters.Remove(parameter); context.SaveChanges(); - return true; } - public ParameterDto? GetParameterById(int parameterId) + public ParameterDto GetParameterById(int parameterId) { var parameter = context.Parameters.Find(parameterId); if (parameter is null) { - return null; + throw new NotFoundException("Parameter not found"); } var parameterDto = MapParameterDtoFromParameter(parameter); diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 188b192e..23aa7656 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -4,16 +4,17 @@ using System.Linq.Expressions; using System.Transactions; using Microsoft.EntityFrameworkCore; +using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Services; public class TagService (ReasnContext context) { - public TagDto? CreateTag(TagDto tagDto) + public TagDto CreateTag(TagDto tagDto) { var tag = context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); if (tag is not null) { - return null; + throw new ObjectExistsException("Tag already exists"); } var newTag = MapTagFromTagDto(tagDto); @@ -22,7 +23,7 @@ public class TagService (ReasnContext context) return tagDto; } - public TagDto? UpdateTag(int tagId, TagDto tagDto, int eventId) + public TagDto UpdateTag(int tagId, TagDto tagDto, int eventId) { using (var scope = new TransactionScope()) { @@ -30,7 +31,7 @@ public class TagService (ReasnContext context) if (tag is null) { - return null; + throw new NotFoundException("Tag not found"); } var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); @@ -59,7 +60,7 @@ public class TagService (ReasnContext context) } else { - return null; + throw new NotFoundException("Tag not found"); } context.SaveChanges(); @@ -68,13 +69,13 @@ public class TagService (ReasnContext context) } } - public bool DeleteTag(int tagId) + public void DeleteTag(int tagId) { var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); if (tag == null) { - return false; + throw new NotFoundException("Tag not found"); } var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); @@ -83,21 +84,19 @@ public bool DeleteTag(int tagId) if (isTagAssociatedWithEvent) { - return false; + throw new ObjectInUseException("Tag is associated with an event"); } context.Tags.Remove(tag); context.SaveChanges(); - - return true; } - public TagDto? GetTagById(int tagId) + public TagDto GetTagById(int tagId) { var tag = context.Tags.Find(tagId); if(tag is null) { - return null; + throw new NotFoundException("Tag not found"); } return MapTagDtoFromTag(tag); From 687a5bdb4699f1118264f2d9694817d8434182ae Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sun, 9 Jun 2024 21:51:42 +0200 Subject: [PATCH 22/65] fixed mappers according to our convension --- .../ReasnAPI/Models/Mappers/EventMapper.cs | 54 +++++++++++ .../ReasnAPI/Models/Mappers/ImageMapper.cs | 36 ++++++++ .../ReasnAPI/Models/Mappers/InterestMapper.cs | 31 +++++++ .../Models/Mappers/ParameterMapper.cs | 30 ++++++ .../ReasnAPI/Models/Mappers/TagMapper.cs | 29 ++++++ .../ReasnAPI/Services/EventService.cs | 92 +++++-------------- .../ReasnAPI/Services/ImageService.cs | 48 +++------- .../ReasnAPI/Services/InterestService.cs | 42 +++------ .../ReasnAPI/Services/ParameterService.cs | 36 ++------ .../ReasnAPI/ReasnAPI/Services/TagService.cs | 38 +++----- 10 files changed, 253 insertions(+), 183 deletions(-) create mode 100644 Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs create mode 100644 Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs create mode 100644 Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs create mode 100644 Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs create mode 100644 Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs new file mode 100644 index 00000000..23293d46 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs @@ -0,0 +1,54 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; + +namespace ReasnAPI.Models.Mappers +{ + public static class EventMapper + { + public static EventDto ToDto(this Event eventToMap) + { + var tags = eventToMap.Tags + .Select(t => new TagDto { Name = t.Name }) + .ToList(); + + var parameters = eventToMap.Parameters + .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) + .ToList(); + + return new EventDto + { + Name = eventToMap.Name, + AddressId = eventToMap.AddressId, + Description = eventToMap.Description, + OrganizerId = eventToMap.OrganizerId, + StartAt = eventToMap.StartAt, + EndAt = eventToMap.EndAt, + CreatedAt = eventToMap.CreatedAt, + UpdatedAt = eventToMap.UpdatedAt, + Slug = eventToMap.Slug, + Status = eventToMap.Status, + Tags = tags, + Parameters = parameters + }; + } + + public static List ToDtoList(this IEnumerable @event) + { + return @event.Select(ToDto).ToList(); + } + public static Event ToEntity(this EventDto eventDto) + { + return new Event + { + Name = eventDto.Name, + AddressId = eventDto.AddressId, + Description = eventDto.Description, + OrganizerId = eventDto.OrganizerId, + StartAt = eventDto.StartAt, + EndAt = eventDto.EndAt, + Status = eventDto.Status + }; + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs new file mode 100644 index 00000000..4911d1b5 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs @@ -0,0 +1,36 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; + +namespace ReasnAPI.Models.Mappers +{ + public static class ImageMapper + { + public static ImageDto ToDto(this Image image) + { + return new ImageDto + { + ImageData = image.ImageData, + ObjectId = image.ObjectId, + ObjectType = image.ObjectType + }; + } + + public static List ToDtoList(this IEnumerable images) + { + return images.Select(ToDto).ToList(); + } + + public static Image ToEntity(this ImageDto imageDto) + { + return new Image + { + ImageData = imageDto.ImageData, + ObjectId = imageDto.ObjectId, + ObjectType = imageDto.ObjectType + }; + } + + + + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs new file mode 100644 index 00000000..be31becc --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs @@ -0,0 +1,31 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; + +namespace ReasnAPI.Models.Mappers +{ + public static class InterestMapper + { + public static InterestDto ToDto(this Interest interest) + { + return new InterestDto + { + Name = interest.Name + }; + } + + public static List ToDtoList(this IEnumerable interests) + { + return interests.Select(ToDto).ToList(); + } + + public static Interest ToEntity(this InterestDto interestDto) + { + return new Interest + { + Name = interestDto.Name + }; + } + } + +} + diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs new file mode 100644 index 00000000..e78366f9 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs @@ -0,0 +1,30 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; + +namespace ReasnAPI.Models.Mappers +{ + public static class ParameterMapper + { + public static ParameterDto ToDto(this Parameter parameter) + { + return new ParameterDto + { + Key = parameter.Key, + Value = parameter.Value + }; + } + public static List ToDtoList(this IEnumerable parameter) + { + return parameter.Select(ToDto).ToList(); + } + public static Parameter ToEntity(this ParameterDto parameterDto) + { + return new Parameter + { + Key = parameterDto.Key, + Value = parameterDto.Value + }; + } + + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs new file mode 100644 index 00000000..9c17c410 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs @@ -0,0 +1,29 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; + +namespace ReasnAPI.Models.Mappers +{ + public static class TagMapper + { + public static TagDto ToDto(this Tag tag) + { + return new TagDto + { + Name = tag.Name + }; + } + + public static List ToDtoList(this IEnumerable tags) + { + return tags.Select(ToDto).ToList(); + } + + public static Tag ToEntity(this TagDto tagDto) + { + return new Tag + { + Name = tagDto.Name + }; + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 67a74c52..e1d99193 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -4,7 +4,8 @@ using System.Linq.Expressions; using System.Linq; using System.Transactions; -using ReasnAPI.Models.Enums; +using ReasnAPI.Models.Enums; +using ReasnAPI.Models.Mappers; using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Services; @@ -16,7 +17,7 @@ public EventDto CreateEvent(EventDto eventDto) { eventDto.Slug = CreateSlug(eventDto); var nowTime = DateTime.UtcNow; - var newEvent = MapEventFromDto(eventDto); + var newEvent = eventDto.ToEntity(); newEvent.CreatedAt = nowTime; newEvent.UpdatedAt = nowTime; newEvent.Slug = eventDto.Slug; @@ -92,12 +93,12 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) .ToList(); - foreach (var tagToRemove in tagsToRemove) - { - eventToUpdate.Tags.Remove(tagToRemove); - } + tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); + + var existingTagsInDb = context.Tags + .Where(tag => newTags.Any(newTag => newTag.Name == tag.Name)) + .ToList(); - var existingTagsInDb = context.Tags.ToList(); var tagsToAdd = newTags .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) .ToList(); @@ -105,12 +106,11 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) context.Tags.AddRange(tagsToAdd); context.SaveChanges(); - var updatedTags = newTags - .Select(newTag => existingTagsInDb.FirstOrDefault(existingTag => existingTag.Name == newTag.Name) ?? - newTag) + var tagsToAttach = newTags + .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) .ToList(); - eventToUpdate.Tags = updatedTags; + tagsToAttach.ForEach(eventToUpdate.Tags.Add); context.SaveChanges(); } @@ -121,32 +121,31 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) .Select(paramDto => new Parameter { Key = paramDto.Key, - Value = paramDto.Value + Value = paramDto.Value }).ToList(); var paramsToRemove = eventToUpdate.Parameters - .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key)) + .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key || newParam.Value != existingParam.Value)) .ToList(); - foreach (var paramToRemove in paramsToRemove) - { - eventToUpdate.Parameters.Remove(paramToRemove); - } + paramsToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); + + var existingParamsInDb = context.Parameters + .Where(param => newParameters.Any(newParam => newParam.Key == param.Key && newParam.Value == param.Value)) + .ToList(); - var existingParamsInDb = context.Parameters.ToList(); var paramsToAdd = newParameters - .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key)) + .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) .ToList(); context.Parameters.AddRange(paramsToAdd); context.SaveChanges(); - var updatedParams = newParameters - .Select(newParam => existingParamsInDb.FirstOrDefault(existingParam => existingParam.Key == newParam.Key) ?? - newParam) + var paramsToAttach = newParameters + .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) .ToList(); - eventToUpdate.Parameters = updatedParams; + paramsToAttach.ForEach(eventToUpdate.Parameters.Add); context.SaveChanges(); } @@ -181,7 +180,7 @@ public void DeleteEvent(int eventId) throw new NotFoundException("Event not found"); } - var eventDto = MapEventDtoFromEvent(eventToReturn); + var eventDto = eventToReturn.ToDto(); return eventDto; } @@ -192,7 +191,7 @@ public IEnumerable GetEventsByFilter(Expression> fil var eventDtos = new List(); foreach (var eventToReturn in events) { - var eventDto = MapEventDtoFromEvent(eventToReturn); + var eventDto = eventToReturn.ToDto(); eventDtos.Add(eventDto); } @@ -207,7 +206,7 @@ public IEnumerable GetAllEvents() var eventDtos = new List(); foreach (var eventToReturn in events) { - var eventDto = MapEventDtoFromEvent(eventToReturn); + var eventDto = eventToReturn.ToDto(); eventDtos.Add(eventDto); } @@ -221,45 +220,4 @@ private string CreateSlug(EventDto eventDto) return slug; } - private Event MapEventFromDto(EventDto eventDto) - { - return new Event - { - Name = eventDto.Name, - AddressId = eventDto.AddressId, - Description = eventDto.Description, - OrganizerId = eventDto.OrganizerId, - StartAt = eventDto.StartAt, - EndAt = eventDto.EndAt, - Status = eventDto.Status - }; - } - - private EventDto MapEventDtoFromEvent(Event eventToMap) - { - var tags = eventToMap.Tags - .Select(t => new TagDto { Name = t.Name }) - .ToList(); - - var parameters = eventToMap.Parameters - .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) - .ToList(); - - return new EventDto - { - Name = eventToMap.Name, - AddressId = eventToMap.AddressId, - Description = eventToMap.Description, - OrganizerId = eventToMap.OrganizerId, - StartAt = eventToMap.StartAt, - EndAt = eventToMap.EndAt, - CreatedAt = eventToMap.CreatedAt, - UpdatedAt = eventToMap.UpdatedAt, - Slug = eventToMap.Slug, - Status = eventToMap.Status, - Tags = tags, - Parameters = parameters - }; - } - } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 2607dbdf..902cf11f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -2,6 +2,7 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using ReasnAPI.Models.Enums; +using ReasnAPI.Models.Mappers; using static System.Net.Mime.MediaTypeNames; using Image = ReasnAPI.Models.Database.Image; using ReasnAPI.Services.Exceptions; @@ -21,7 +22,7 @@ public List CreateImages(List imageDtos) continue; } - var newImage = MapImageFromImageDto(imageDto); + var newImage = imageDto.ToEntity(); newImages.Add(newImage); } @@ -84,58 +85,35 @@ public ImageDto GetImageById(int id) throw new NotFoundException("Image not found"); } - var imageDto = MapImageDtoFromImage(image); + var imageDto = image.ToDto(); return imageDto; } - public IEnumerable GetAllImages() + public IEnumerable GetAllImages() { - var images = context.Images.ToList(); - - var imageDtos = images.Select(image => MapImageDtoFromImage(image)).AsEnumerable(); - - return imageDtos; + return context.Images + .ToDtoList() + .AsEnumerable(); } public IEnumerable GetImagesByFilter(Expression> filter) { - var images = context.Images.Where(filter).ToList(); - - var imageDtos = images.Select(image => MapImageDtoFromImage(image)).AsEnumerable(); - - return imageDtos; + return context.Images + .Where(filter) + .ToDtoList() + .AsEnumerable(); } - public IEnumerable GetImagesByEventIdAndType(int eventId) + public IEnumerable GetImagesByEventId(int eventId) { var images = context.Images .Where(image => image.ObjectType == ObjectType.Event && image.ObjectId == eventId) .ToList(); - var imageDtos = images.Select(image => MapImageDtoFromImage(image)).AsEnumerable(); + var imageDtos = images.Select(image => image.ToDto()).AsEnumerable(); return imageDtos; - } - - private ImageDto MapImageDtoFromImage(Image image) - { - return new ImageDto - { - ImageData = image.ImageData, - ObjectId = image.ObjectId, - ObjectType = image.ObjectType - }; - } - - private Image MapImageFromImageDto(ImageDto imageDto) - { - return new Image - { - ImageData = imageDto.ImageData, - ObjectId = imageDto.ObjectId, - ObjectType = imageDto.ObjectType - }; } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index a8281072..a86d85c2 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -1,8 +1,9 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using ReasnAPI.Services.Exceptions; -using System.Linq.Expressions; - +using System.Linq.Expressions; +using ReasnAPI.Models.Mappers; + namespace ReasnAPI.Services; public class InterestService(ReasnContext context) @@ -15,7 +16,7 @@ public InterestDto CreateInterest(InterestDto interestDto) throw new ObjectExistsException("Interest already exists"); } - var newInterest = MapInterestFromInterestDto(interestDto); + var newInterest = interestDto.ToEntity(); context.Interests.Add(newInterest); context.SaveChanges(); @@ -63,41 +64,22 @@ public InterestDto GetInterestById(int interestId) throw new NotFoundException("Interest not found"); } - var interestDto = MapInterestDtoFromInterest(interest); - - return interestDto; + return interest.ToDto(); } public IEnumerable GetAllInterests() { - var interests = context.Interests.ToList(); - - return interests.Select(interest => MapInterestDtoFromInterest(interest)).AsEnumerable(); + return context.Interests + .ToDtoList() + .AsEnumerable(); } public IEnumerable GetInterestsByFilter(Expression> filter) { - var interests = context.Interests.Where(filter).ToList(); - - var interestDtos = interests.Select(interest => MapInterestDtoFromInterest(interest)).AsEnumerable(); - - return interestDtos; - } - - private InterestDto MapInterestDtoFromInterest(Interest interest) - { - return new InterestDto - { - Name = interest.Name - }; - } - - private Interest MapInterestFromInterestDto(InterestDto interestDto) - { - return new Interest - { - Name = interestDto.Name - }; + return context.Interests + .Where(filter) + .ToDtoList() + .AsEnumerable(); } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index a60e0e8b..9ce8632b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -2,6 +2,7 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; +using ReasnAPI.Models.Mappers; using ReasnAPI.Services.Exceptions; namespace ReasnAPI.Services; @@ -15,7 +16,7 @@ public ParameterDto CreateParameter(ParameterDto parameterDto) throw new ObjectExistsException("Parameter already exists"); } - var newParameter = MapParameterFromParameterDto(parameterDto); + var newParameter = parameterDto.ToEntity(); context.Parameters.Add(newParameter); context.SaveChanges(); return parameterDto; @@ -79,43 +80,24 @@ public ParameterDto GetParameterById(int parameterId) throw new NotFoundException("Parameter not found"); } - var parameterDto = MapParameterDtoFromParameter(parameter); - + var parameterDto = parameter.ToDto(); return parameterDto; } public IEnumerable GetAllParameters() { - var parameters = context.Parameters.ToList(); - - return parameters.Select(parameter => MapParameterDtoFromParameter(parameter)) + return context.Parameters + .ToDtoList() .AsEnumerable(); } public IEnumerable GetParametersByFilter(Expression> filter) - { - var parameters = context.Parameters.Where(filter).ToList(); - - return parameters.Select(parameter => MapParameterDtoFromParameter(parameter)) + { + return context.Parameters + .Where(filter) + .ToDtoList() .AsEnumerable(); } - private Parameter MapParameterFromParameterDto(ParameterDto parameterDto) - { - return new Parameter - { - Key = parameterDto.Key, - Value = parameterDto.Value - }; - } - - private ParameterDto MapParameterDtoFromParameter(Parameter parameter) - { - return new ParameterDto - { - Key = parameter.Key, - Value = parameter.Value - }; - } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 23aa7656..4d75ec8c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -5,6 +5,8 @@ using System.Transactions; using Microsoft.EntityFrameworkCore; using ReasnAPI.Services.Exceptions; +using System.Net; +using ReasnAPI.Models.Mappers; namespace ReasnAPI.Services; public class TagService (ReasnContext context) @@ -17,8 +19,7 @@ public TagDto CreateTag(TagDto tagDto) throw new ObjectExistsException("Tag already exists"); } - var newTag = MapTagFromTagDto(tagDto); - context.Tags.Add(newTag); + context.Tags.Add(tagDto.ToEntity()); context.SaveChanges(); return tagDto; } @@ -42,7 +43,7 @@ public TagDto UpdateTag(int tagId, TagDto tagDto, int eventId) if (eventTags.Count > 1 || (eventTags.Count == 1 && eventTags[0].Id != eventId)) { // Create new tag, associate it with the event, and remove the old association - var newTag = MapTagFromTagDto(tagDto); + var newTag = tagDto.ToEntity(); context.Tags.Add(newTag); context.SaveChanges(); @@ -99,35 +100,24 @@ public TagDto GetTagById(int tagId) throw new NotFoundException("Tag not found"); } - return MapTagDtoFromTag(tag); + return tag.ToDto(); } public IEnumerable GetAllTags() - { - var tags = context.Tags.ToList(); - return tags.Select(tag => MapTagDtoFromTag(tag)).AsEnumerable(); + { + return context.Tags + .ToDtoList() + .AsEnumerable(); } public IEnumerable GetTagsByFilter(Expression> filter) { - var tags = context.Tags.Where(filter).ToList(); - return tags.Select(tag => MapTagDtoFromTag(tag)).AsEnumerable(); - } - - private TagDto MapTagDtoFromTag(Tag tag) - { - return new TagDto - { - Name = tag.Name - }; + return context.Tags + .Where(filter) + .ToDtoList() + .AsEnumerable(); } - private Tag MapTagFromTagDto(TagDto tagDto) - { - return new Tag - { - Name = tagDto.Name - }; - } + } \ No newline at end of file From 17ef41a211e8fea946c2fe6c8ce3daaea339219e Mon Sep 17 00:00:00 2001 From: Maciej Koperdowski Date: Sun, 9 Jun 2024 20:38:24 +0200 Subject: [PATCH 23/65] Changed exceptions directory location --- .../Services/EventServicesTest.cs | 2 +- .../Services/ImageServiceTests.cs | 2 +- .../Services/InterestServiceTests.cs | 2 +- .../Services/ParameterServiceTests.cs | 2 +- .../Services/TagServiceTests.cs | 2 +- .../Exceptions/ObjectExistsException.cs | 2 +- .../Exceptions/ObjectInUseException.cs | 2 +- .../ReasnAPI/Services/EventService.cs | 28 +++++------ .../Services/Exceptions/NotFoundException.cs | 10 ---- .../ReasnAPI/Services/ImageService.cs | 2 +- .../ReasnAPI/Services/InterestService.cs | 47 ++++++++++--------- .../ReasnAPI/Services/ParameterService.cs | 16 ++++--- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 2 +- 13 files changed, 56 insertions(+), 63 deletions(-) rename Server/ReasnAPI/ReasnAPI/{Services => }/Exceptions/ObjectExistsException.cs (84%) rename Server/ReasnAPI/ReasnAPI/{Services => }/Exceptions/ObjectInUseException.cs (83%) delete mode 100644 Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 9a11c313..0f540ec7 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -9,7 +9,7 @@ using Moq; using Moq.EntityFrameworkCore; using ReasnAPI.Models.Enums; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Tests.Services diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 951acee7..15189c5c 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -10,7 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Tests.Services diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index 2f17fdb9..ce878fd9 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -10,7 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Tests.Services { diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index 642d6bf7..ea5f7843 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -10,7 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Tests.Services { diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 54aecb08..713e6a3d 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -10,7 +10,7 @@ using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Tests.Services { diff --git a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs b/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs similarity index 84% rename from Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs rename to Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs index f6dfa0ac..d96e63c7 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectExistsException.cs +++ b/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs @@ -1,4 +1,4 @@ -namespace ReasnAPI.Services.Exceptions; +namespace ReasnAPI.Exceptions; public class ObjectExistsException : Exception { public ObjectExistsException() : base() { } diff --git a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs b/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs similarity index 83% rename from Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs rename to Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs index 182a65dd..cd222ce9 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/ObjectInUseException.cs +++ b/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs @@ -1,4 +1,4 @@ -namespace ReasnAPI.Services.Exceptions; +namespace ReasnAPI.Exceptions; public class ObjectInUseException : Exception { diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index e1d99193..25d7f075 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -1,17 +1,17 @@ -using Microsoft.EntityFrameworkCore; -using ReasnAPI.Models.Database; -using ReasnAPI.Models.DTOs; -using System.Linq.Expressions; -using System.Linq; +using Microsoft.EntityFrameworkCore; +using ReasnAPI.Exceptions; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using System.Linq.Expressions; using System.Transactions; using ReasnAPI.Models.Enums; using ReasnAPI.Models.Mappers; using ReasnAPI.Services.Exceptions; -namespace ReasnAPI.Services; +namespace ReasnAPI.Services; public class EventService(ReasnContext context) -{ - public EventDto CreateEvent(EventDto eventDto) +{ + public EventDto CreateEvent(EventDto eventDto) { using (var scope = new TransactionScope()) { @@ -19,9 +19,9 @@ public EventDto CreateEvent(EventDto eventDto) var nowTime = DateTime.UtcNow; var newEvent = eventDto.ToEntity(); newEvent.CreatedAt = nowTime; - newEvent.UpdatedAt = nowTime; + newEvent.UpdatedAt = nowTime; newEvent.Slug = eventDto.Slug; - + context.Events.Add(newEvent); context.SaveChanges(); @@ -54,7 +54,7 @@ public EventDto CreateEvent(EventDto eventDto) scope.Complete(); } - return eventDto; + return eventDto; } public EventDto UpdateEvent(int eventId, EventDto eventDto) @@ -62,7 +62,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) using (var scope = new TransactionScope()) { var eventToUpdate = context.Events.FirstOrDefault(r => r.Id == eventId); - + if (eventToUpdate is null) { throw new NotFoundException("Event not found"); @@ -155,7 +155,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) return eventDto; } - public void DeleteEvent(int eventId) + public void DeleteEvent(int eventId) { using (var scope = new TransactionScope()) { @@ -170,7 +170,7 @@ public void DeleteEvent(int eventId) context.SaveChanges(); scope.Complete(); } - } + } public EventDto? GetEventById(int eventId) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs b/Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs deleted file mode 100644 index 1eadad1e..00000000 --- a/Server/ReasnAPI/ReasnAPI/Services/Exceptions/NotFoundException.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ReasnAPI.Services.Exceptions; - -public class NotFoundException : Exception -{ - public NotFoundException() { } - - public NotFoundException(string message) : base(message) { } - - public NotFoundException(string message, Exception innerException) : base(message, innerException) { } -} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 902cf11f..bbd99b57 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -5,7 +5,7 @@ using ReasnAPI.Models.Mappers; using static System.Net.Mime.MediaTypeNames; using Image = ReasnAPI.Models.Database.Image; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Services; public class ImageService(ReasnContext context) diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index a86d85c2..aa1d67a1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -1,13 +1,14 @@ -using ReasnAPI.Models.Database; -using ReasnAPI.Models.DTOs; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; +using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; +using ReasnAPI.Services.Exceptions; using System.Linq.Expressions; using ReasnAPI.Models.Mappers; -namespace ReasnAPI.Services; - +namespace ReasnAPI.Services; + public class InterestService(ReasnContext context) -{ +{ public InterestDto CreateInterest(InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Name == interestDto.Name); @@ -21,9 +22,9 @@ public InterestDto CreateInterest(InterestDto interestDto) context.Interests.Add(newInterest); context.SaveChanges(); return interestDto; - } - - public InterestDto UpdateInterest(int interestId, InterestDto interestDto) + } + + public InterestDto UpdateInterest(int interestId, InterestDto interestDto) { var interest = context.Interests.FirstOrDefault(r => r.Id == interestId); if (interest is null) @@ -36,9 +37,9 @@ public InterestDto UpdateInterest(int interestId, InterestDto interestDto) context.Interests.Update(interest); context.SaveChanges(); return interestDto; - } - - public void DeleteInterest(int id) + } + + public void DeleteInterest(int id) { var interest = context.Interests.FirstOrDefault(r => r.Id == id); if (interest is null) @@ -47,16 +48,16 @@ public void DeleteInterest(int id) } var eventInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id); - if (eventInterest is not null) + if (eventInterest is not null) { throw new ObjectInUseException("Interest is in use"); } context.Interests.Remove(interest); context.SaveChanges(); - } - - public InterestDto GetInterestById(int interestId) + } + + public InterestDto GetInterestById(int interestId) { var interest = context.Interests.Find(interestId); if (interest is null) @@ -65,21 +66,21 @@ public InterestDto GetInterestById(int interestId) } return interest.ToDto(); - } - - public IEnumerable GetAllInterests() + } + + public IEnumerable GetAllInterests() { return context.Interests .ToDtoList() .AsEnumerable(); - } - + } + public IEnumerable GetInterestsByFilter(Expression> filter) { return context.Interests .Where(filter) .ToDtoList() .AsEnumerable(); - } - + } + } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 9ce8632b..afb32ee9 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -1,4 +1,6 @@ -using ReasnAPI.Models.Database; +using Microsoft.EntityFrameworkCore; +using ReasnAPI.Exceptions; +using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; @@ -27,19 +29,19 @@ public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); if (parameter is null) - { - throw new NotFoundException("Parameter not found"); + { + throw new NotFoundException("Parameter not found"); } var parameters = context.Events.Include(p => p.Parameters); var parameterCheck = parameters.FirstOrDefault(r => r.Parameters.Any(p => p.Id == parameterId)); - + if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated { throw new ObjectInUseException("Parameter is associated with an event"); } - + parameter.Key = parameterDto.Key; parameter.Value = parameterDto.Value; context.Parameters.Update(parameter); @@ -63,7 +65,7 @@ public void DeleteParameter(int parameterId) var parameterCheck = eventsWithParameters .Any(e => e.Parameters.Any(p => p.Id == parameterId)); - if (parameterCheck) + if (parameterCheck) { throw new ObjectInUseException("Parameter is associated with an event"); } @@ -92,7 +94,7 @@ public IEnumerable GetAllParameters() } public IEnumerable GetParametersByFilter(Expression> filter) - { + { return context.Parameters .Where(filter) .ToDtoList() diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 4d75ec8c..c96952b3 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -104,7 +104,7 @@ public TagDto GetTagById(int tagId) } public IEnumerable GetAllTags() - { + { return context.Tags .ToDtoList() .AsEnumerable(); From 5cc818afa6e511c9c6d3fd9b4c90cc74e21b5a66 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sun, 9 Jun 2024 21:55:54 +0200 Subject: [PATCH 24/65] fixed build error --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 2 +- Server/ReasnAPI/ReasnAPI/Services/InterestService.cs | 2 +- Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs | 2 +- Server/ReasnAPI/ReasnAPI/Services/TagService.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 25d7f075..b7fe6d4e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -6,7 +6,7 @@ using System.Transactions; using ReasnAPI.Models.Enums; using ReasnAPI.Models.Mappers; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Services; public class EventService(ReasnContext context) diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index aa1d67a1..9f12692d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -1,7 +1,7 @@ using ReasnAPI.Exceptions; using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; using System.Linq.Expressions; using ReasnAPI.Models.Mappers; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index afb32ee9..fc881d04 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -5,7 +5,7 @@ using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; using ReasnAPI.Models.Mappers; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; namespace ReasnAPI.Services; public class ParameterService(ReasnContext context) diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index c96952b3..c78e3127 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -4,7 +4,7 @@ using System.Linq.Expressions; using System.Transactions; using Microsoft.EntityFrameworkCore; -using ReasnAPI.Services.Exceptions; +using ReasnAPI.Exceptions; using System.Net; using ReasnAPI.Models.Mappers; From 3021123cf037c1ee3b0481c4ad8cf8fcae6d5e94 Mon Sep 17 00:00:00 2001 From: Maciej Koperdowski Date: Mon, 10 Jun 2024 01:13:52 +0200 Subject: [PATCH 25/65] Exceptions changes --- .../ReasnAPI/Exceptions/ObjectExistsException.cs | 9 --------- .../ReasnAPI/Exceptions/ObjectInUseException.cs | 10 ---------- Server/ReasnAPI/ReasnAPI/Services/InterestService.cs | 5 ++--- Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs | 7 +++---- Server/ReasnAPI/ReasnAPI/Services/TagService.cs | 7 ++----- 5 files changed, 7 insertions(+), 31 deletions(-) delete mode 100644 Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs delete mode 100644 Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs diff --git a/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs b/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs deleted file mode 100644 index d96e63c7..00000000 --- a/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectExistsException.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace ReasnAPI.Exceptions; -public class ObjectExistsException : Exception -{ - public ObjectExistsException() : base() { } - - public ObjectExistsException(string message) : base(message) { } - - public ObjectExistsException(string message, Exception innerException) : base(message, innerException) { } -} diff --git a/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs b/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs deleted file mode 100644 index cd222ce9..00000000 --- a/Server/ReasnAPI/ReasnAPI/Exceptions/ObjectInUseException.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ReasnAPI.Exceptions; - -public class ObjectInUseException : Exception -{ - public ObjectInUseException() { } - - public ObjectInUseException(string message) : base(message) { } - - public ObjectInUseException(string message, Exception innerException) : base(message, innerException) { } -} \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 9f12692d..40931307 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -1,7 +1,6 @@ using ReasnAPI.Exceptions; using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -using ReasnAPI.Exceptions; using System.Linq.Expressions; using ReasnAPI.Models.Mappers; @@ -14,7 +13,7 @@ public InterestDto CreateInterest(InterestDto interestDto) var interest = context.Interests.FirstOrDefault(r => r.Name == interestDto.Name); if (interest is not null) { - throw new ObjectExistsException("Interest already exists"); + throw new BadRequestException("Interest already exists"); } var newInterest = interestDto.ToEntity(); @@ -50,7 +49,7 @@ public void DeleteInterest(int id) var eventInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id); if (eventInterest is not null) { - throw new ObjectInUseException("Interest is in use"); + throw new BadRequestException("Interest is in use"); } context.Interests.Remove(interest); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index fc881d04..7272b861 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -5,7 +5,6 @@ using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; using ReasnAPI.Models.Mappers; -using ReasnAPI.Exceptions; namespace ReasnAPI.Services; public class ParameterService(ReasnContext context) @@ -15,7 +14,7 @@ public ParameterDto CreateParameter(ParameterDto parameterDto) var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key && r.Value == parameterDto.Value); if (parameter is not null) { - throw new ObjectExistsException("Parameter already exists"); + throw new BadRequestException("Parameter already exists"); } var newParameter = parameterDto.ToEntity(); @@ -39,7 +38,7 @@ public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated { - throw new ObjectInUseException("Parameter is associated with an event"); + throw new BadRequestException("Parameter is associated with an event"); } parameter.Key = parameterDto.Key; @@ -67,7 +66,7 @@ public void DeleteParameter(int parameterId) if (parameterCheck) { - throw new ObjectInUseException("Parameter is associated with an event"); + throw new BadRequestException("Parameter is associated with an event"); } context.Parameters.Remove(parameter); diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index c78e3127..9667fc8b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -16,7 +16,7 @@ public TagDto CreateTag(TagDto tagDto) var tag = context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); if (tag is not null) { - throw new ObjectExistsException("Tag already exists"); + throw new BadRequestException("Tag already exists"); } context.Tags.Add(tagDto.ToEntity()); @@ -85,7 +85,7 @@ public void DeleteTag(int tagId) if (isTagAssociatedWithEvent) { - throw new ObjectInUseException("Tag is associated with an event"); + throw new BadRequestException("Tag is associated with an event"); } context.Tags.Remove(tag); @@ -117,7 +117,4 @@ public IEnumerable GetTagsByFilter(Expression> filter) .ToDtoList() .AsEnumerable(); } - - - } \ No newline at end of file From bf25afd84c20bcb5035b98f7245719d8de648220 Mon Sep 17 00:00:00 2001 From: Maciej Koperdowski Date: Mon, 10 Jun 2024 01:15:29 +0200 Subject: [PATCH 26/65] Unit tests fix --- .../ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs | 4 ++-- .../ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs | 4 ++-- Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index ce878fd9..e00a71b7 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -48,7 +48,7 @@ public void CreateInterest_InterestExists_InterestNotCreated() var interestService = new InterestService(mockContext.Object); - Assert.ThrowsException(() => interestService.CreateInterest(interestDto)); + Assert.ThrowsException(() => interestService.CreateInterest(interestDto)); } [TestMethod] @@ -178,7 +178,7 @@ public void DeleteInterest_InterestHasUserInterests_NothingDeleted() new UserInterest { InterestId = 1, UserId = 1 } }); - Assert.ThrowsException(() => new InterestService(mockContext.Object).DeleteInterest(1)); + Assert.ThrowsException(() => new InterestService(mockContext.Object).DeleteInterest(1)); mockContext.Verify(c => c.Interests.Remove(It.IsAny()), Times.Never); mockContext.Verify(c => c.SaveChanges(), Times.Never); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index ea5f7843..bfb64628 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -50,7 +50,7 @@ public void CreateParameter_ParameterExists_ParameterNotCreated() var parameterService = new ParameterService(mockContext.Object); - Assert.ThrowsException(() => parameterService.CreateParameter(parameterDto)); + Assert.ThrowsException(() => parameterService.CreateParameter(parameterDto)); } [TestMethod] @@ -119,7 +119,7 @@ public void UpdateParameter_ParameterInUse_NullReturned() var parameterService = new ParameterService(mockContext.Object); - Assert.ThrowsException(() => parameterService.UpdateParameter(1, parameterDto)); + Assert.ThrowsException(() => parameterService.UpdateParameter(1, parameterDto)); mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 713e6a3d..44f60d94 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -48,7 +48,7 @@ public void CreateTag_TagExists_TagNotCreated() var tagService = new TagService(mockContext.Object); - Assert.ThrowsException(() => tagService.CreateTag(tagDto)); + Assert.ThrowsException(() => tagService.CreateTag(tagDto)); } [TestMethod] @@ -191,7 +191,7 @@ public void DeleteTag_TagInUse_NothingHappens() var tagService = new TagService(mockContext.Object); - Assert.ThrowsException(() => tagService.DeleteTag(1)); + Assert.ThrowsException(() => tagService.DeleteTag(1)); Assert.AreEqual(1, tags.Count); // Ensure the tag was not removed mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } From a52a1d33940807cf7517c2d642b8910c3694f46a Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 13:46:31 +0200 Subject: [PATCH 27/65] updated services --- .../ReasnAPI/Services/EventService.cs | 20 +++++++++++++++++-- .../ReasnAPI/Services/ImageService.cs | 4 ++-- .../ReasnAPI/Services/InterestService.cs | 4 ++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index b7fe6d4e..d4792269 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -61,7 +61,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) { using (var scope = new TransactionScope()) { - var eventToUpdate = context.Events.FirstOrDefault(r => r.Id == eventId); + var eventToUpdate = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Id == eventId); if (eventToUpdate is null) { @@ -159,13 +159,29 @@ public void DeleteEvent(int eventId) { using (var scope = new TransactionScope()) { - var eventToDelete = context.Events.FirstOrDefault(r => r.Id == eventId); + var eventToDelete = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Id == eventId); if (eventToDelete is null) { throw new NotFoundException("Event not found"); } + foreach (var tag in eventToDelete.Tags.ToList()) + { + if (!context.Events.Any(e => e.Tags.Any(t => t.Name == tag.Name) && e.Id != eventId)) + { + context.Tags.Remove(tag); + } + } + + foreach (var parameter in eventToDelete.Parameters.ToList()) + { + if (!context.Events.Any(e => e.Parameters.Any(p => p.Key == parameter.Key && p.Value == parameter.Value) && e.Id != eventId)) + { + context.Parameters.Remove(parameter); + } + } + context.Events.Remove(eventToDelete); context.SaveChanges(); scope.Complete(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index bbd99b57..9e583374 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -29,12 +29,12 @@ public List CreateImages(List imageDtos) if (newImages.Any()) { - var objectType = newImages.First().ObjectType; + var objectType = newImages[0].ObjectType; if (objectType == ObjectType.User && newImages.Count == 1) { - context.Images.Add(newImages.First()); + context.Images.Add(newImages[0]); } else if (objectType == ObjectType.Event) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 40931307..79459467 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -46,8 +46,8 @@ public void DeleteInterest(int id) throw new NotFoundException("Interest not found"); } - var eventInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id); - if (eventInterest is not null) + var userInterest = context.UserInterests.FirstOrDefault(r => r.InterestId == id); + if (userInterest is not null) { throw new BadRequestException("Interest is in use"); } From c64189f093c4ade393c07490aa200299ea0cc6be Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 13:52:31 +0200 Subject: [PATCH 28/65] Slug creating method updated --- .../ReasnAPI/ReasnAPI/Services/EventService.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index d4792269..8eb7dbae 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -15,12 +15,13 @@ public EventDto CreateEvent(EventDto eventDto) { using (var scope = new TransactionScope()) { - eventDto.Slug = CreateSlug(eventDto); - var nowTime = DateTime.UtcNow; + + var createdTime = DateTime.UtcNow; var newEvent = eventDto.ToEntity(); - newEvent.CreatedAt = nowTime; - newEvent.UpdatedAt = nowTime; - newEvent.Slug = eventDto.Slug; + newEvent.CreatedAt = createdTime; + newEvent.UpdatedAt = createdTime; + newEvent.Slug = CreateSlug(eventDto, createdTime); + context.Events.Add(newEvent); context.SaveChanges(); @@ -230,10 +231,11 @@ public IEnumerable GetAllEvents() return eventDtos; } - private string CreateSlug(EventDto eventDto) + private string CreateSlug(EventDto eventDto, DateTime createdTime) { var slug = eventDto.Name.ToLower().Replace(" ", "-"); - return slug; + var timestamp = createdTime.Ticks; + return $"{slug}-{timestamp}"; } } \ No newline at end of file From 342ab16c2126b5c5bf2b55f00de0c86c14faf35d Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 15:10:53 +0200 Subject: [PATCH 29/65] Update ImageService.cs --- Server/ReasnAPI/ReasnAPI/Services/ImageService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 9e583374..0b3c442a 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -16,7 +16,7 @@ public List CreateImages(List imageDtos) foreach (var imageDto in imageDtos) { - var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectType == imageDto.ObjectType); + var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectType == imageDto.ObjectType && r.ImageData == imageDto.ImageData); if (image is not null) { continue; From 68f4c174d3370730f12bc7c4d6036685194543a7 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 17:00:54 +0200 Subject: [PATCH 30/65] Update ImageService.cs --- .../ReasnAPI/Services/ImageService.cs | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 0b3c442a..c42d72d4 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -40,29 +40,61 @@ public List CreateImages(List imageDtos) { context.Images.AddRange(newImages); } - - + context.SaveChanges(); } return imageDtos; - } - - public ImageDto UpdateImage(int imageId, ImageDto imageDto) + } + + public void UpdateImages(int objectId, List imageDtos) { - var image = context.Images.FirstOrDefault(r => r.Id == imageId); - if (image is null) + var objectType = imageDtos[0].ObjectType; + + if (objectType == ObjectType.User) { - throw new NotFoundException("Image not found"); + if (imageDtos.Count != 1) + { + throw new ArgumentException("For User type, only one image can be updated"); + } + + var image = context.Images.FirstOrDefault(r => r.ObjectId == objectId && r.ObjectType == ObjectType.User); + if (image is null) + { + throw new NotFoundException("Image not found"); + } + + image.ImageData = imageDtos[0].ImageData; + + context.Images.Update(image); } + else if (objectType == ObjectType.Event) + { + var images = context.Images.Where(r => r.ObjectId == objectId && r.ObjectType == ObjectType.Event).ToList(); + + // Remove images that are not in the new list + foreach (var image in images) + { + if (!imageDtos.Any(dto => dto.ImageData == image.ImageData)) + { + context.Images.Remove(image); + } + } - image.ObjectId = imageDto.ObjectId; - image.ImageData = imageDto.ImageData; - image.ObjectType = imageDto.ObjectType; + // Add new images that are not in the database + var newImages = new List(); + foreach (var imageDto in imageDtos) + { + if (!images.Any(img => img.ImageData == imageDto.ImageData)) + { + var newImage = imageDto.ToEntity(); + newImages.Add(newImage); + } + } + context.Images.AddRange(newImages); + } - context.Images.Update(image); context.SaveChanges(); - return imageDto; } public void DeleteImage(int id) From faa050acf49982a4d5077bbeec2cf916dd327c04 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 17:05:43 +0200 Subject: [PATCH 31/65] updated image services and tests also updated slug method for event services --- .../Services/ImageServiceTests.cs | 47 ++++++++++++++++--- .../ReasnAPI/Services/EventService.cs | 18 ++++++- .../ReasnAPI/Services/ImageService.cs | 5 ++ 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 15189c5c..cf94d030 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -11,6 +11,7 @@ using Moq; using Moq.EntityFrameworkCore; using ReasnAPI.Exceptions; +using ReasnAPI.Models.Enums; namespace ReasnAPI.Tests.Services @@ -120,40 +121,72 @@ public void GetImageById_ImageDoesNotExist_NullReturned() } [TestMethod] - public void UpdateImage_ImageExists_ImageUpdated() + public void UpdateImages_UserType_ImageUpdated() { var imageDto = new ImageDto { + ObjectId = 1, + ObjectType = ObjectType.User, ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } }; var mockContext = new Mock(); mockContext.Setup(c => c.Images).ReturnsDbSet(new List - { new Image { Id = 1, ImageData = new byte[] { } } }); + { new Image { ObjectId = 1, ObjectType = ObjectType.User, ImageData = new byte[] { } } }); var imageService = new ImageService(mockContext.Object); - var result = imageService.UpdateImage(1, imageDto); + imageService.UpdateImages(1, new List { imageDto }); - CollectionAssert.AreEqual(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, result.ImageData); + mockContext.Verify(c => c.SaveChanges(), Times.Once); } [TestMethod] + public void UpdateImages_EventType_ImagesUpdated() + { + var imageDtos = new List + { + new ImageDto + { + ObjectId = 1, + ObjectType = ObjectType.Event, + ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } + }, + new ImageDto + { + ObjectId = 1, + ObjectType = ObjectType.Event, + ImageData = new byte[] { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 } + } + }; + + var mockContext = new Mock(); + mockContext.Setup(c => c.Images).ReturnsDbSet(new List + { new Image { ObjectId = 1, ObjectType = ObjectType.Event, ImageData = new byte[] { } } }); + + var imageService = new ImageService(mockContext.Object); - public void UpdateImage_ImageDoesNotExist_NullReturned() + imageService.UpdateImages(1, imageDtos); + + mockContext.Verify(c => c.SaveChanges(), Times.Once); + } + + [TestMethod] + public void UpdateImages_ImageDoesNotExist_ExceptionThrown() { var imageDto = new ImageDto { + ObjectId = 1, + ObjectType = ObjectType.User, ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } }; var mockContext = new Mock(); - mockContext.Setup(c => c.Images).ReturnsDbSet(new List()); var imageService = new ImageService(mockContext.Object); - Assert.ThrowsException(() => imageService.UpdateImage(1, imageDto)); + Assert.ThrowsException(() => imageService.UpdateImages(1, new List { imageDto })); } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 8eb7dbae..23fec76e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -7,6 +7,7 @@ using ReasnAPI.Models.Enums; using ReasnAPI.Models.Mappers; using ReasnAPI.Exceptions; +using System.Text.RegularExpressions; namespace ReasnAPI.Services; public class EventService(ReasnContext context) @@ -233,8 +234,21 @@ public IEnumerable GetAllEvents() private string CreateSlug(EventDto eventDto, DateTime createdTime) { - var slug = eventDto.Name.ToLower().Replace(" ", "-"); - var timestamp = createdTime.Ticks; + var slug = eventDto.Name.ToLower(); + slug = slug.Trim(); + slug = Regex.Replace(slug, @"\s+", " "); + slug = Regex.Replace(slug, " ", "-"); + slug = Regex.Replace(slug, @"[^a-z0-9-]", ""); + slug = Regex.Replace(slug, "-+", "-"); + + var timestamp = createdTime.Ticks.ToString(); + var maxSlugLength = 128 - timestamp.Length - 1; // -1 for the dash between slug and timestamp + + if (slug.Length > maxSlugLength) + { + slug = slug.Substring(0, maxSlugLength); + } + return $"{slug}-{timestamp}"; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index c42d72d4..a5780a77 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -49,6 +49,11 @@ public List CreateImages(List imageDtos) public void UpdateImages(int objectId, List imageDtos) { + if (!imageDtos.Any()) + { + throw new ArgumentException("No images provided"); + } + var objectType = imageDtos[0].ObjectType; if (objectType == ObjectType.User) From 7bda1bf0852adc7f5d328f715825c99633fb0fba Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:01:53 +0200 Subject: [PATCH 32/65] event service delete method updated --- .../ReasnAPI/Services/EventService.cs | 20 ++++++++++++++++--- .../ReasnAPI/Services/ParameterService.cs | 1 - 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 23fec76e..98f7371e 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -161,7 +161,7 @@ public void DeleteEvent(int eventId) { using (var scope = new TransactionScope()) { - var eventToDelete = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Id == eventId); + var eventToDelete = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e=>e.Participants).Include(e => e.Comments).FirstOrDefault(e => e.Id == eventId); if (eventToDelete is null) { @@ -170,20 +170,34 @@ public void DeleteEvent(int eventId) foreach (var tag in eventToDelete.Tags.ToList()) { + eventToDelete.Tags.Remove(tag);// remove the association first if (!context.Events.Any(e => e.Tags.Any(t => t.Name == tag.Name) && e.Id != eventId)) { - context.Tags.Remove(tag); + + // Check if the tag is associated with any other event + if (!context.Events.Any(e => e.Tags.Contains(tag))) + { + context.Tags.Remove(tag); // then remove the tag + } } } foreach (var parameter in eventToDelete.Parameters.ToList()) { + eventToDelete.Parameters.Remove(parameter); // remove the association first if (!context.Events.Any(e => e.Parameters.Any(p => p.Key == parameter.Key && p.Value == parameter.Value) && e.Id != eventId)) { - context.Parameters.Remove(parameter); + // Check if the parameter is associated with any other event + if (!context.Events.Any(e => e.Parameters.Contains(parameter))) + { + context.Parameters.Remove(parameter); // then remove the parameter + } } } + context.Comments.RemoveRange(eventToDelete.Comments); + context.Participants.RemoveRange(eventToDelete.Participants); + context.Events.Remove(eventToDelete); context.SaveChanges(); scope.Complete(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 7272b861..78133037 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -3,7 +3,6 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; -using Microsoft.EntityFrameworkCore; using ReasnAPI.Models.Mappers; namespace ReasnAPI.Services; From 0f2a8c0229d0ea8d630b3530b79ddfde2e213c38 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:23:56 +0200 Subject: [PATCH 33/65] refactor and fixes --- .../Services/EventServicesTest.cs | 105 ++++---- .../Services/ImageServiceTests.cs | 4 +- .../ReasnAPI/Models/Database/ReasnContext.cs | 4 +- .../Models/Mappers/ParameterMapper.cs | 6 + .../ReasnAPI/Models/Mappers/TagMapper.cs | 5 + .../ReasnAPI/Services/EventService.cs | 230 ++++++++++-------- .../ReasnAPI/Services/ImageService.cs | 8 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 2 - 8 files changed, 198 insertions(+), 166 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 0f540ec7..a4ceac95 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -333,59 +333,64 @@ public void DeleteEvent_EventExists_EventDeleted() { var mockContext = new Mock(); mockContext.Setup(c => c.Events).ReturnsDbSet(new List - { - new Event() - { - Id = 1, - Name = "name", - Slug = "name", - AddressId = 1, - Description = "description", - OrganizerId = 1, - StartAt = DateTime.Now, - EndAt = DateTime.Now, - CreatedAt = DateTime.Now, - UpdatedAt = DateTime.Now, - Status = EventStatus.Completed, - } - }); + { + new Event() + { + Id = 1, + Name = "name", + Slug = "name", + AddressId = 1, + Description = "description", + OrganizerId = 1, + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Status = EventStatus.Completed, + Tags = new List { new Tag { Id = 1, Name = "name" } }, + Parameters = new List { new Parameter { Key = "key", Value = "value" } }, + Comments = new List { new Comment { Id = 1, Content = "content" } }, + Participants = new List { new Participant { Id = 1, UserId = 1 } } + } + }); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List - { - new Tag() - { - Id = 1, - Name = "name" - } - }); + { + new Tag() + { + Id = 1, + Name = "name" + } + }); mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
- { - new Address() - { - Id = 1, - City = "city", - Country = "country", - State = "state", - Street = "street", - ZipCode = "test123" - } - }); + { + new Address() + { + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", + ZipCode = "test123" + } + }); mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ - new User() - { - Id = 1, - Name = "test", - Email = "test@wp.pl", - AddressId = 1, - CreatedAt = DateTime.Now, - IsActive = true, - Role = UserRole.User, - Password ="test123", - Phone = "123123123", - Surname ="test", - Username ="test", - UpdatedAt =DateTime.Now }}); - - + new User() + { + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, + CreatedAt = DateTime.Now, + IsActive = true, + Role = UserRole.User, + Password ="test123", + Phone = "123123123", + Surname ="test", + Username ="test", + UpdatedAt =DateTime.Now }}); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); var eventService = new EventService(mockContext.Object); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index cf94d030..f81a145e 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -33,7 +33,7 @@ public void CreateImage_ImageDoesNotExist_ImageCreated() var imageService = new ImageService(mockContext.Object); List imagedtoslist = new List(); - imagedtoslist.Append(imageDto); + imagedtoslist.Add(imageDto); // Use Add instead of Append var result = imageService.CreateImages(imagedtoslist); Assert.IsNotNull(result); @@ -53,7 +53,7 @@ public void CreateImage_ImageExists_ImageNotCreated() var imageService = new ImageService(mockContext.Object); List imagedtoslist = new List(); - imagedtoslist.Append(imageDto); + imagedtoslist.Add(imageDto); // Use Add instead of Append var result = imageService.CreateImages(imagedtoslist); diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs index 8b09a291..f24cda17 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs @@ -147,7 +147,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.ImageData).HasColumnName("image_data"); entity.Property(e => e.ObjectId).HasColumnName("object_id"); - entity.Property(i => i.ObjectType).HasColumnName("object_type"); + entity.Property(e => e.ObjectType).HasColumnName("object_type"); }); modelBuilder.Entity(entity => @@ -253,7 +253,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.Surname).HasColumnName("surname"); entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); entity.Property(e => e.Username).HasColumnName("username"); - entity.Property(u => u.Role).HasColumnName("role"); + entity.Property(e => e.Role).HasColumnName("role"); entity.HasOne(d => d.Address).WithMany(p => p.Users) .HasForeignKey(d => d.AddressId) diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs index e78366f9..8c52fea6 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs @@ -17,6 +17,12 @@ public static List ToDtoList(this IEnumerable parameter { return parameter.Select(ToDto).ToList(); } + + public static List ToEntityList(this IEnumerable tags) + { + return tags.Select(ToEntity).ToList(); + } + public static Parameter ToEntity(this ParameterDto parameterDto) { return new Parameter diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs index 9c17c410..5a6d126b 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs @@ -18,6 +18,11 @@ public static List ToDtoList(this IEnumerable tags) return tags.Select(ToDto).ToList(); } + public static List ToEntityList(this IEnumerable tags) + { + return tags.Select(ToEntity).ToList(); + } + public static Tag ToEntity(this TagDto tagDto) { return new Tag diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 98f7371e..7eedd6bc 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -4,9 +4,7 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using System.Transactions; -using ReasnAPI.Models.Enums; using ReasnAPI.Models.Mappers; -using ReasnAPI.Exceptions; using System.Text.RegularExpressions; namespace ReasnAPI.Services; @@ -27,30 +25,8 @@ public EventDto CreateEvent(EventDto eventDto) context.Events.Add(newEvent); context.SaveChanges(); - var eventId = newEvent.Id; - - if (eventDto.Tags is not null && eventDto.Tags.Any()) - { - var newTags = eventDto.Tags - .Where(t => !context.Tags.Any(x => x.Name == t.Name)) - .Select(t => new Tag { Name = t.Name }) - .ToList(); - - context.Tags.AddRange(newTags); - newEvent.Tags = newTags; - } - - if (eventDto.Parameters is not null && eventDto.Parameters.Any()) - { - var newParameters = eventDto.Parameters - .Where(p => !context.Parameters.Any(x => x.Key == p.Key && x.Value == p.Value)) - .Select(p => new Parameter { Key = p.Key, Value = p.Value }) - .ToList(); - - context.Parameters.AddRange(newParameters); - - newEvent.Parameters = newParameters; - } + AddTags(eventDto, newEvent); + AddParameters(eventDto, newEvent); context.SaveChanges(); scope.Complete(); @@ -58,6 +34,32 @@ public EventDto CreateEvent(EventDto eventDto) return eventDto; } + private void AddTags(EventDto eventDto, Event newEvent) + { + if (eventDto.Tags is not null && eventDto.Tags.Any()) + { + var newTags = eventDto.Tags + .Where(t => !context.Tags.Any(x => x.Name == t.Name)) + .Select(t => new Tag { Name = t.Name }) + .ToList(); + + context.Tags.AddRange(newTags); + newEvent.Tags = newTags; + } + } + private void AddParameters(EventDto eventDto, Event newEvent) + { + if (eventDto.Parameters is not null && eventDto.Parameters.Any()) + { + var newParameters = eventDto.Parameters + .Where(p => !context.Parameters.Any(x => x.Key == p.Key && x.Value == p.Value)) + .Select(p => new Parameter { Key = p.Key, Value = p.Value }) + .ToList(); + + context.Parameters.AddRange(newParameters); + newEvent.Parameters = newParameters; + } + } public EventDto UpdateEvent(int eventId, EventDto eventDto) { @@ -82,126 +84,138 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) context.Events.Update(eventToUpdate); context.SaveChanges(); - if (eventDto.Tags is not null) - { + UpdateTags(eventDto, eventToUpdate); + UpdateParameters(eventDto, eventToUpdate); + + context.SaveChanges(); + scope.Complete(); + } - var newTags = eventDto.Tags - .Select(tagDto => new Tag - { - Name = tagDto.Name, - }).ToList(); + return eventDto; + } - var tagsToRemove = eventToUpdate.Tags - .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) - .ToList(); + private void UpdateTags(EventDto eventDto, Event eventToUpdate) + { + if (eventDto.Tags is not null) + { + var newTags = eventDto.Tags.ToEntityList(); - tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); + var tagsToRemove = eventToUpdate.Tags + .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) + .ToList(); - var existingTagsInDb = context.Tags - .Where(tag => newTags.Any(newTag => newTag.Name == tag.Name)) - .ToList(); + tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); - var tagsToAdd = newTags - .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); + var existingTagsInDb = context.Tags + .Where(tag => newTags.Any(newTag => newTag.Name == tag.Name)) + .ToList(); - context.Tags.AddRange(tagsToAdd); - context.SaveChanges(); + var tagsToAdd = newTags + .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) + .ToList(); - var tagsToAttach = newTags - .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); + context.Tags.AddRange(tagsToAdd); + context.SaveChanges(); - tagsToAttach.ForEach(eventToUpdate.Tags.Add); + var tagsToAttach = newTags + .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) + .ToList(); - context.SaveChanges(); - } + tagsToAttach.ForEach(eventToUpdate.Tags.Add); - if (eventDto.Parameters is not null) - { - var newParameters = eventDto.Parameters - .Select(paramDto => new Parameter - { - Key = paramDto.Key, - Value = paramDto.Value - }).ToList(); + context.SaveChanges(); + } + } - var paramsToRemove = eventToUpdate.Parameters - .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key || newParam.Value != existingParam.Value)) - .ToList(); + private void UpdateParameters(EventDto eventDto, Event eventToUpdate) + { + if (eventDto.Parameters is not null) + { + var newParameters = eventDto.Parameters.ToEntityList(); - paramsToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); + var paramsToRemove = eventToUpdate.Parameters + .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key || newParam.Value != existingParam.Value)) + .ToList(); - var existingParamsInDb = context.Parameters - .Where(param => newParameters.Any(newParam => newParam.Key == param.Key && newParam.Value == param.Value)) - .ToList(); + paramsToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); - var paramsToAdd = newParameters - .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) - .ToList(); + var existingParamsInDb = context.Parameters + .Where(param => newParameters.Any(newParam => newParam.Key == param.Key && newParam.Value == param.Value)) + .ToList(); - context.Parameters.AddRange(paramsToAdd); - context.SaveChanges(); + var paramsToAdd = newParameters + .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) + .ToList(); - var paramsToAttach = newParameters - .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) - .ToList(); + context.Parameters.AddRange(paramsToAdd); + context.SaveChanges(); - paramsToAttach.ForEach(eventToUpdate.Parameters.Add); + var paramsToAttach = newParameters + .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) + .ToList(); - context.SaveChanges(); - } - scope.Complete(); - } + paramsToAttach.ForEach(eventToUpdate.Parameters.Add); - return eventDto; + context.SaveChanges(); + } } public void DeleteEvent(int eventId) { using (var scope = new TransactionScope()) { - var eventToDelete = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e=>e.Participants).Include(e => e.Comments).FirstOrDefault(e => e.Id == eventId); + var eventToDelete = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Participants).Include(e => e.Comments).FirstOrDefault(e => e.Id == eventId); if (eventToDelete is null) { throw new NotFoundException("Event not found"); } - foreach (var tag in eventToDelete.Tags.ToList()) + RemoveTags(eventToDelete, eventId); + RemoveParameters(eventToDelete, eventId); + RemoveComments(eventToDelete); + RemoveParticipants(eventToDelete); + + context.Events.Remove(eventToDelete); + context.SaveChanges(); + scope.Complete(); + } + } + + private void RemoveTags(Event eventToDelete, int eventId) + { + foreach (var tag in eventToDelete.Tags.ToList()) + { + eventToDelete.Tags.Remove(tag); // remove the association first + // Check if the tag is associated with any other event + if (!context.Events.Any(e => e.Tags.Any(t => t.Name == tag.Name) && e.Id != eventId) && !context.Events.Any(e => e.Tags.Contains(tag))) { - eventToDelete.Tags.Remove(tag);// remove the association first - if (!context.Events.Any(e => e.Tags.Any(t => t.Name == tag.Name) && e.Id != eventId)) - { - - // Check if the tag is associated with any other event - if (!context.Events.Any(e => e.Tags.Contains(tag))) - { - context.Tags.Remove(tag); // then remove the tag - } - } + context.Tags.Remove(tag); // then remove the tag } + } + } - foreach (var parameter in eventToDelete.Parameters.ToList()) + private void RemoveParameters(Event eventToDelete, int eventId) + { + foreach (var parameter in eventToDelete.Parameters.ToList()) + { + eventToDelete.Parameters.Remove(parameter); // remove the association first + // Check if the parameter is associated with any other event + if (!context.Events.Any(e => e.Parameters.Any(p => p.Key == parameter.Key && p.Value == parameter.Value) && e.Id != eventId) && !context.Events.Any(e => e.Parameters.Contains(parameter))) { - eventToDelete.Parameters.Remove(parameter); // remove the association first - if (!context.Events.Any(e => e.Parameters.Any(p => p.Key == parameter.Key && p.Value == parameter.Value) && e.Id != eventId)) - { - // Check if the parameter is associated with any other event - if (!context.Events.Any(e => e.Parameters.Contains(parameter))) - { - context.Parameters.Remove(parameter); // then remove the parameter - } - } + context.Parameters.Remove(parameter); // then remove the parameter } + } + } - context.Comments.RemoveRange(eventToDelete.Comments); - context.Participants.RemoveRange(eventToDelete.Participants); + private void RemoveComments(Event eventToDelete) + { + context.Comments.RemoveRange(eventToDelete.Comments); + } - context.Events.Remove(eventToDelete); - context.SaveChanges(); - scope.Complete(); - } + private void RemoveParticipants(Event eventToDelete) + { + context.Participants.RemoveRange(eventToDelete.Participants); } public EventDto? GetEventById(int eventId) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index a5780a77..42a43919 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -14,6 +14,11 @@ public List CreateImages(List imageDtos) { var newImages = new List(); + if (imageDtos[0].ObjectType == ObjectType.User && imageDtos.Count > 1) + { + throw new ArgumentException("For User type, only one image can be created"); + } + foreach (var imageDto in imageDtos) { var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectType == imageDto.ObjectType && r.ImageData == imageDto.ImageData); @@ -30,8 +35,7 @@ public List CreateImages(List imageDtos) if (newImages.Any()) { var objectType = newImages[0].ObjectType; - - + if (objectType == ObjectType.User && newImages.Count == 1) { context.Images.Add(newImages[0]); diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 9667fc8b..75968731 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -1,11 +1,9 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -using System.Linq; using System.Linq.Expressions; using System.Transactions; using Microsoft.EntityFrameworkCore; using ReasnAPI.Exceptions; -using System.Net; using ReasnAPI.Models.Mappers; namespace ReasnAPI.Services; From 59b3beaac91a820ffc947a8c0aff3985e19a7f99 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:25:39 +0200 Subject: [PATCH 34/65] refactor --- .../Services/EventServicesTest.cs | 79 +------------------ .../ReasnAPI.Tests/Services/FakeDbSet.cs | 9 +-- .../Services/InterestServiceTests.cs | 22 +----- .../Services/ParameterServiceTests.cs | 22 +----- .../Services/TagServiceTests.cs | 22 +----- 5 files changed, 5 insertions(+), 149 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index a4ceac95..9c5f74e2 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ReasnAPI.Models.DTOs; +using ReasnAPI.Models.DTOs; using ReasnAPI.Services; using ReasnAPI.Models.Database; using Moq; @@ -17,78 +12,6 @@ namespace ReasnAPI.Tests.Services [TestClass] public class EventServicesTest { - // [TestMethod] - // public void CreateEvent_EventDoesNotExist_EventCreated() - // { - // // Arrange - // var tagDto = new TagDto - // { - // Name = "testTag" - // }; - // var tagList = new List { tagDto }; - - // var eventDto = new EventDto - // { - // Name = "name", - // AddressId = 1, - // Description = "description", - // OrganizerId = 1, - // StartAt = DateTime.Now, - // EndAt = DateTime.Now, - // CreatedAt = DateTime.Now, - // UpdatedAt = DateTime.Now, - // Status = EventStatus.Approved, - // Tags = tagList, - // }; - - // var mockContext = new Mock(); - // var events = new List(); - // var tags = new List(); - - // mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); - // mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); - // mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - - // mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
- //{ - // new Address - // { - // Id = 1, - // City = "city", - // Country = "country", - // State = "state", - // Street = "street", - // ZipCode = "test123" - // } - //}); - - // mockContext.Setup(c => c.Users).ReturnsDbSet(new List - //{ - // new User - // { - // Id = 1, - // Name = "test", - // Email = "test@wp.pl", - // AddressId = 1, - // CreatedAt = DateTime.Now, - // IsActive = true, - // Role = UserRole.Admin, - // Password = "test123", - // Phone = "123123123", - // Surname = "test", - // Username = "test", - // UpdatedAt = DateTime.Now - // } - //}); - - // var eventService = new EventService(mockContext.Object); - - // // Act - // var result = eventService.CreateEvent(eventDto); - - // // Assert - // Assert.IsNotNull(result); - // } [TestMethod] public void UpdateEvent_EventExists_EventUpdated() diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs index f8cb8506..49312afb 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; namespace ReasnAPI.Tests.Services @@ -36,8 +31,6 @@ public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToke public override Microsoft.EntityFrameworkCore.Metadata.IEntityType EntityType => throw new NotImplementedException(); - // Implement other methods... - private class AsyncEnumerator : IAsyncEnumerator { private readonly IEnumerator _enumerator; diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index e00a71b7..ecc24fe6 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -1,13 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using ReasnAPI.Models.Database; +using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using ReasnAPI.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Linq; -using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; using ReasnAPI.Exceptions; @@ -88,19 +81,6 @@ public void GetInterestById_InterestDoesNotExist_NullReturned() Assert.ThrowsException(() => interestService.GetInterestById(1)); } - //[TestMethod] - //public void GetInterestById_InterestExists_InterestReturned() - //{ - // var mockContext = new Mock(); - // mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest {Id = 1, Name = "TestInterest"} }); - - // var interestService = new InterestService(mockContext.Object); - - // var result = interestService.GetInterestById(1); - - // Assert.AreEqual("TestInterest", result.Name); - //} - [TestMethod] public void UpdateInterest_InterestExists_InterestUpdated() { diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index bfb64628..733d9971 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -1,13 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using ReasnAPI.Models.Database; +using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using ReasnAPI.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Linq; -using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; using ReasnAPI.Exceptions; @@ -134,19 +127,6 @@ public void GetParameterById_ParameterDoesNotExist_NullReturned() Assert.ThrowsException(() => parameterService.GetParameterById(1)); } - //[TestMethod] - //public void GetParameterById_ParameterExists_ParameterReturned() - //{ - // var mockContext = new Mock(); - // mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); - - // var parameterService = new ParameterService(mockContext.Object); - - // var result = parameterService.GetParameterById(1); - - // Assert.AreEqual("TestValue", result.Value); - //} - [TestMethod] public void DeleteParameter_ParameterExists_ParameterDeleted() { diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 44f60d94..11d1c8da 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -1,13 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using ReasnAPI.Models.Database; +using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using ReasnAPI.Services; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Linq; -using System.Linq.Expressions; using Moq; using Moq.EntityFrameworkCore; using ReasnAPI.Exceptions; @@ -77,19 +70,6 @@ public void GetAllTags_TagNotExists_EmptyListReturned() Assert.AreEqual(0, result.Count); } - //[TestMethod] - //public void GetTagById_TagExists_TagReturned() - //{ - // var mockContext = new Mock(); - // mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); - - // var tagService = new TagService(mockContext.Object); - - // var result = tagService.GetTagById(1); - - // Assert.AreEqual("TestTag", result.Name); - //} - [TestMethod] public void GetTagById_TagDoesNotExist_NullReturned() { From 2a1b4a7acd75b14e9f4548143f547229e3eb0380 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:28:44 +0200 Subject: [PATCH 35/65] Update EventService.cs --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 7eedd6bc..a36843c3 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -39,24 +39,23 @@ private void AddTags(EventDto eventDto, Event newEvent) if (eventDto.Tags is not null && eventDto.Tags.Any()) { var newTags = eventDto.Tags - .Where(t => !context.Tags.Any(x => x.Name == t.Name)) - .Select(t => new Tag { Name = t.Name }) + .Select(t => context.Tags.FirstOrDefault(x => x.Name == t.Name) ?? new Tag { Name = t.Name }) .ToList(); - context.Tags.AddRange(newTags); + context.Tags.AddRange(newTags.Where(t => t.Id == 0)); // Add only new tags to the context newEvent.Tags = newTags; } } + private void AddParameters(EventDto eventDto, Event newEvent) { if (eventDto.Parameters is not null && eventDto.Parameters.Any()) { var newParameters = eventDto.Parameters - .Where(p => !context.Parameters.Any(x => x.Key == p.Key && x.Value == p.Value)) - .Select(p => new Parameter { Key = p.Key, Value = p.Value }) + .Select(p => context.Parameters.FirstOrDefault(x => x.Key == p.Key && x.Value == p.Value) ?? new Parameter { Key = p.Key, Value = p.Value }) .ToList(); - context.Parameters.AddRange(newParameters); + context.Parameters.AddRange(newParameters.Where(p => p.Id == 0)); // Add only new parameters to the context newEvent.Parameters = newParameters; } } From 8385ed82d6972cd61e676eade18d560c161d83a5 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:31:47 +0200 Subject: [PATCH 36/65] last refactor --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index a36843c3..c797a32b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -80,11 +80,9 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) eventToUpdate.UpdatedAt = DateTime.Now; eventToUpdate.Status = eventDto.Status; - context.Events.Update(eventToUpdate); - context.SaveChanges(); - UpdateTags(eventDto, eventToUpdate); UpdateParameters(eventDto, eventToUpdate); + context.Events.Update(eventToUpdate); context.SaveChanges(); scope.Complete(); From 90305e2e45c4636c473bdcd832a3843d11e1c5a5 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:33:59 +0200 Subject: [PATCH 37/65] removing spaces --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index c797a32b..1fd86b0d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -14,13 +14,11 @@ public EventDto CreateEvent(EventDto eventDto) { using (var scope = new TransactionScope()) { - var createdTime = DateTime.UtcNow; var newEvent = eventDto.ToEntity(); newEvent.CreatedAt = createdTime; newEvent.UpdatedAt = createdTime; newEvent.Slug = CreateSlug(eventDto, createdTime); - context.Events.Add(newEvent); context.SaveChanges(); From 3f102720699eb2530c964d1f42dff2cdab47b422 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 18:58:35 +0200 Subject: [PATCH 38/65] added geteventbyslug method to eventservice --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 1fd86b0d..cef05779 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -226,6 +226,19 @@ private void RemoveParticipants(Event eventToDelete) return eventDto; } + public EventDto? GetEventBySlug(string slug) + { + var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Slug == slug); + if (eventToReturn is null) + { + throw new NotFoundException("Event not found"); + } + + var eventDto = eventToReturn.ToDto(); + + return eventDto; + } + public IEnumerable GetEventsByFilter(Expression> filter) { var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Where(filter).ToList(); From a941d1d6182e98475bb4991f2b89ea42b7db9212 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 22:44:31 +0200 Subject: [PATCH 39/65] Update ImageService.cs --- .../ReasnAPI/ReasnAPI/Services/ImageService.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 42a43919..576de1d8 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -129,6 +129,22 @@ public ImageDto GetImageById(int id) var imageDto = image.ToDto(); return imageDto; + } + + public List GetImagesByObjectId(int objectId, ObjectType objectType) + { + var images = context.Images + .Where(image => image.ObjectId == objectId && image.ObjectType == objectType) + .ToList(); + + if (!images.Any()) + { + throw new NotFoundException("Images not found"); + } + + var imageDtos = images.Select(image => image.ToDto()).ToList(); + + return imageDtos; } public IEnumerable GetAllImages() From 9df5dcb22f3282c22bb4c4a610f618b7c7dbff0f Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 22:47:06 +0200 Subject: [PATCH 40/65] refactor --- Server/ReasnAPI/ReasnAPI/Services/ImageService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 576de1d8..24f7a01a 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -2,9 +2,7 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using ReasnAPI.Models.Enums; -using ReasnAPI.Models.Mappers; -using static System.Net.Mime.MediaTypeNames; -using Image = ReasnAPI.Models.Database.Image; +using ReasnAPI.Models.Mappers; using ReasnAPI.Exceptions; namespace ReasnAPI.Services; From fc7e7c77830543833c8a7757b5074d8c38937036 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 22:48:19 +0200 Subject: [PATCH 41/65] changed list to Enumerable --- Server/ReasnAPI/ReasnAPI/Services/ImageService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 24f7a01a..f1cd4a78 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -129,7 +129,7 @@ public ImageDto GetImageById(int id) return imageDto; } - public List GetImagesByObjectId(int objectId, ObjectType objectType) + public IEnumerable GetImagesByObjectId(int objectId, ObjectType objectType) { var images = context.Images .Where(image => image.ObjectId == objectId && image.ObjectType == objectType) @@ -140,7 +140,7 @@ public List GetImagesByObjectId(int objectId, ObjectType objectType) throw new NotFoundException("Images not found"); } - var imageDtos = images.Select(image => image.ToDto()).ToList(); + var imageDtos = images.Select(image => image.ToDto()).AsEnumerable(); return imageDtos; } From 55bc25c9f4d8a5137d87349e3a1cc60399eac237 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 22:49:10 +0200 Subject: [PATCH 42/65] changed list to enumerable --- Server/ReasnAPI/ReasnAPI/Services/ImageService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index f1cd4a78..b819d368 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -8,7 +8,7 @@ namespace ReasnAPI.Services; public class ImageService(ReasnContext context) { - public List CreateImages(List imageDtos) + public IEnumerable CreateImages(List imageDtos) { var newImages = new List(); @@ -46,7 +46,7 @@ public List CreateImages(List imageDtos) context.SaveChanges(); } - return imageDtos; + return imageDtos.AsEnumerable(); } public void UpdateImages(int objectId, List imageDtos) From 1e4316ac873e618da25ac2904846135cf188763f Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 10 Jun 2024 23:09:32 +0200 Subject: [PATCH 43/65] folder refactor --- .../{Models => }/Mappers/EventMapper.cs | 2 +- .../{Models => }/Mappers/ImageMapper.cs | 2 +- .../ReasnAPI/Mappers/InterestMapper.cs | 39 ++++++++++--------- .../{Models => }/Mappers/ParameterMapper.cs | 2 +- .../{Models => }/Mappers/TagMapper.cs | 2 +- .../ReasnAPI/Models/Mappers/InterestMapper.cs | 31 --------------- .../ReasnAPI/Services/EventService.cs | 2 +- .../ReasnAPI/Services/ImageService.cs | 7 ++-- .../ReasnAPI/Services/InterestService.cs | 2 +- .../ReasnAPI/Services/ParameterService.cs | 2 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 2 +- 11 files changed, 33 insertions(+), 60 deletions(-) rename Server/ReasnAPI/ReasnAPI/{Models => }/Mappers/EventMapper.cs (95%) rename Server/ReasnAPI/ReasnAPI/{Models => }/Mappers/ImageMapper.cs (92%) rename Server/ReasnAPI/ReasnAPI/{Models => }/Mappers/ParameterMapper.cs (92%) rename Server/ReasnAPI/ReasnAPI/{Models => }/Mappers/TagMapper.cs (91%) delete mode 100644 Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs similarity index 95% rename from Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs rename to Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs index 23293d46..a95ddca6 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/EventMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs @@ -1,7 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -namespace ReasnAPI.Models.Mappers +namespace ReasnAPI.Mappers { public static class EventMapper { diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs similarity index 92% rename from Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs rename to Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs index 4911d1b5..943bef5a 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ImageMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs @@ -1,7 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -namespace ReasnAPI.Models.Mappers +namespace ReasnAPI.Mappers { public static class ImageMapper { diff --git a/Server/ReasnAPI/ReasnAPI/Mappers/InterestMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/InterestMapper.cs index f6f75f72..e16863fe 100644 --- a/Server/ReasnAPI/ReasnAPI/Mappers/InterestMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/InterestMapper.cs @@ -1,28 +1,31 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -namespace ReasnAPI.Mappers; - -public static class InterestMapper +namespace ReasnAPI.Mappers { - public static InterestDto ToDto(this Interest interest) + public static class InterestMapper { - return new InterestDto + public static InterestDto ToDto(this Interest interest) { - Name = interest.Name - }; - } + return new InterestDto + { + Name = interest.Name + }; + } - public static List ToDtoList(this IEnumerable interests) - { - return interests.Select(ToDto).ToList(); - } + public static List ToDtoList(this IEnumerable interests) + { + return interests.Select(ToDto).ToList(); + } - public static Interest ToEntity(this InterestDto interestDto) - { - return new Interest + public static Interest ToEntity(this InterestDto interestDto) { - Name = interestDto.Name - }; + return new Interest + { + Name = interestDto.Name + }; + } } -} \ No newline at end of file + +} + diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/ParameterMapper.cs similarity index 92% rename from Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs rename to Server/ReasnAPI/ReasnAPI/Mappers/ParameterMapper.cs index 8c52fea6..76cdf96b 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/ParameterMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/ParameterMapper.cs @@ -1,7 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -namespace ReasnAPI.Models.Mappers +namespace ReasnAPI.Mappers { public static class ParameterMapper { diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/TagMapper.cs similarity index 91% rename from Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs rename to Server/ReasnAPI/ReasnAPI/Mappers/TagMapper.cs index 5a6d126b..c39d6508 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/TagMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/TagMapper.cs @@ -1,7 +1,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -namespace ReasnAPI.Models.Mappers +namespace ReasnAPI.Mappers { public static class TagMapper { diff --git a/Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs b/Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs deleted file mode 100644 index be31becc..00000000 --- a/Server/ReasnAPI/ReasnAPI/Models/Mappers/InterestMapper.cs +++ /dev/null @@ -1,31 +0,0 @@ -using ReasnAPI.Models.Database; -using ReasnAPI.Models.DTOs; - -namespace ReasnAPI.Models.Mappers -{ - public static class InterestMapper - { - public static InterestDto ToDto(this Interest interest) - { - return new InterestDto - { - Name = interest.Name - }; - } - - public static List ToDtoList(this IEnumerable interests) - { - return interests.Select(ToDto).ToList(); - } - - public static Interest ToEntity(this InterestDto interestDto) - { - return new Interest - { - Name = interestDto.Name - }; - } - } - -} - diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index cef05779..e0aeb2eb 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -4,8 +4,8 @@ using ReasnAPI.Models.DTOs; using System.Linq.Expressions; using System.Transactions; -using ReasnAPI.Models.Mappers; using System.Text.RegularExpressions; +using ReasnAPI.Mappers; namespace ReasnAPI.Services; public class EventService(ReasnContext context) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index b819d368..b2498796 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -1,9 +1,10 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; -using System.Linq.Expressions; -using ReasnAPI.Models.Enums; -using ReasnAPI.Models.Mappers; +using System.Linq.Expressions; using ReasnAPI.Exceptions; +using ReasnAPI.Models.Enums; +using ReasnAPI.Mappers; + namespace ReasnAPI.Services; public class ImageService(ReasnContext context) diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 79459467..964ee451 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -2,7 +2,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; -using ReasnAPI.Models.Mappers; +using ReasnAPI.Mappers; namespace ReasnAPI.Services; diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 78133037..cddacfb7 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -3,7 +3,7 @@ using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using System.Linq.Expressions; -using ReasnAPI.Models.Mappers; +using ReasnAPI.Mappers; namespace ReasnAPI.Services; public class ParameterService(ReasnContext context) diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 75968731..5f829a2c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -4,7 +4,7 @@ using System.Transactions; using Microsoft.EntityFrameworkCore; using ReasnAPI.Exceptions; -using ReasnAPI.Models.Mappers; +using ReasnAPI.Mappers; namespace ReasnAPI.Services; public class TagService (ReasnContext context) From dc4ffa9957de74b2ab439f815901e15c44164e78 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Tue, 11 Jun 2024 19:50:21 +0200 Subject: [PATCH 44/65] updated services --- .../Services/EventServicesTest.cs | 3 + .../Services/ImageServiceTests.cs | 4 +- .../Services/TagServiceTests.cs | 4 +- .../ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs | 2 - .../ReasnAPI/Services/EventService.cs | 181 ++++++++---------- .../ReasnAPI/Services/ImageService.cs | 35 +++- .../ReasnAPI/Services/InterestService.cs | 15 ++ .../ReasnAPI/Services/ParameterService.cs | 23 +++ .../ReasnAPI/ReasnAPI/Services/TagService.cs | 41 +++- 9 files changed, 193 insertions(+), 115 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 9c5f74e2..4f0a2d56 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -77,6 +77,9 @@ public void UpdateEvent_EventExists_EventUpdated() Username ="test", UpdatedAt =DateTime.Now }}); + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); + mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); var eventService = new EventService(mockContext.Object); var result = eventService.UpdateEvent(1,eventDto); diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index f81a145e..0de2de08 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -200,7 +200,7 @@ public void DeleteImage_ImageExists_ImageDeleted() var imageService = new ImageService(mockContext.Object); - imageService.DeleteImage(1); + imageService.DeleteImageById(1); mockContext.Verify(c => c.SaveChanges(), Times.Once); } @@ -215,7 +215,7 @@ public void DeleteImage_ImageDoesNotExist_NothingDeleted() var imageService = new ImageService(mockContext.Object); - Assert.ThrowsException(() => imageService.DeleteImage(1)); + Assert.ThrowsException(() => imageService.DeleteImageById(1)); } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 11d1c8da..88d85028 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -99,7 +99,7 @@ public void UpdateTag_TagExists_TagUpdated() var tagService = new TagService(mockContext.Object); // Act - var result = tagService.UpdateTag(1, tagDto, eventId); + var result = tagService.UpdateTagForEvent(1, tagDto, eventId); // Assert Assert.IsNotNull(result); @@ -122,7 +122,7 @@ public void UpdateTag_TagDoesNotExist_NullReturned() mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId } }); var tagService = new TagService(mockContext.Object); - Assert.ThrowsException(() => tagService.UpdateTag(1, tagDto, eventId)); + Assert.ThrowsException(() => tagService.UpdateTagForEvent(1, tagDto, eventId)); mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called } diff --git a/Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs index 943bef5a..1e449b3d 100644 --- a/Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/ImageMapper.cs @@ -30,7 +30,5 @@ public static Image ToEntity(this ImageDto imageDto) }; } - - } } diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index e0aeb2eb..eb432d5a 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -10,6 +10,9 @@ namespace ReasnAPI.Services; public class EventService(ReasnContext context) { + private readonly ParameterService _parameterService = new ParameterService(context); + private readonly TagService _tagService = new TagService(context); + public EventDto CreateEvent(EventDto eventDto) { using (var scope = new TransactionScope()) @@ -22,9 +25,18 @@ public EventDto CreateEvent(EventDto eventDto) context.Events.Add(newEvent); context.SaveChanges(); + if (eventDto.Tags != null) + { + var newTags = eventDto.Tags.ToEntityList(); + AttatchTagsToEvent(newTags, newEvent); + } - AddTags(eventDto, newEvent); - AddParameters(eventDto, newEvent); + if (eventDto.Parameters != null) + { + var newParameters = eventDto.Parameters.ToEntityList(); + AttachParametersToEvent(newParameters, newEvent); + } + context.SaveChanges(); scope.Complete(); @@ -32,31 +44,6 @@ public EventDto CreateEvent(EventDto eventDto) return eventDto; } - private void AddTags(EventDto eventDto, Event newEvent) - { - if (eventDto.Tags is not null && eventDto.Tags.Any()) - { - var newTags = eventDto.Tags - .Select(t => context.Tags.FirstOrDefault(x => x.Name == t.Name) ?? new Tag { Name = t.Name }) - .ToList(); - - context.Tags.AddRange(newTags.Where(t => t.Id == 0)); // Add only new tags to the context - newEvent.Tags = newTags; - } - } - - private void AddParameters(EventDto eventDto, Event newEvent) - { - if (eventDto.Parameters is not null && eventDto.Parameters.Any()) - { - var newParameters = eventDto.Parameters - .Select(p => context.Parameters.FirstOrDefault(x => x.Key == p.Key && x.Value == p.Value) ?? new Parameter { Key = p.Key, Value = p.Value }) - .ToList(); - - context.Parameters.AddRange(newParameters.Where(p => p.Id == 0)); // Add only new parameters to the context - newEvent.Parameters = newParameters; - } - } public EventDto UpdateEvent(int eventId, EventDto eventDto) { @@ -78,8 +65,34 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) eventToUpdate.UpdatedAt = DateTime.Now; eventToUpdate.Status = eventDto.Status; - UpdateTags(eventDto, eventToUpdate); - UpdateParameters(eventDto, eventToUpdate); + if (eventDto.Tags == null) + { + DetachTagsFromEvent(eventToUpdate.Tags.ToList(), eventToUpdate); + } + else + { + var newTags = eventDto.Tags.ToEntityList(); + var tagsToRemove = eventToUpdate.Tags + .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) + .ToList(); + DetachTagsFromEvent(tagsToRemove, eventToUpdate); + AttatchTagsToEvent(newTags, eventToUpdate); + } + + if (eventDto.Parameters == null) + { + DetachParametersFromEvent(eventToUpdate.Parameters.ToList(), eventToUpdate); + } + else + { + var newParameters = eventDto.Parameters.ToEntityList(); + var paramsToRemove = eventToUpdate.Parameters + .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key || newParam.Value != existingParam.Value)) + .ToList(); + DetachParametersFromEvent(paramsToRemove, eventToUpdate); + AttachParametersToEvent(newParameters, eventToUpdate); + } + context.Events.Update(eventToUpdate); context.SaveChanges(); @@ -89,70 +102,46 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) return eventDto; } - private void UpdateTags(EventDto eventDto, Event eventToUpdate) + private void AttatchTagsToEvent(List tagsToAdd, Event eventToUpdate) { - if (eventDto.Tags is not null) - { - var newTags = eventDto.Tags.ToEntityList(); - - var tagsToRemove = eventToUpdate.Tags - .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) - .ToList(); - - tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); - - var existingTagsInDb = context.Tags - .Where(tag => newTags.Any(newTag => newTag.Name == tag.Name)) - .ToList(); - - var tagsToAdd = newTags - .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); - - context.Tags.AddRange(tagsToAdd); - context.SaveChanges(); + _tagService.AddTagsFromList(tagsToAdd); - var tagsToAttach = newTags - .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); + var tagsToAttach = tagsToAdd + .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) + .ToList(); - tagsToAttach.ForEach(eventToUpdate.Tags.Add); + tagsToAttach.ForEach(eventToUpdate.Tags.Add); - context.SaveChanges(); - } + context.SaveChanges(); } - private void UpdateParameters(EventDto eventDto, Event eventToUpdate) + private void DetachTagsFromEvent(List tagsToRemove, Event eventToUpdate) { - if (eventDto.Parameters is not null) - { - var newParameters = eventDto.Parameters.ToEntityList(); + tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); + context.SaveChanges(); - var paramsToRemove = eventToUpdate.Parameters - .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key || newParam.Value != existingParam.Value)) - .ToList(); - - paramsToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); + _tagService.RemoveTagsNotInAnyEvent(); + } - var existingParamsInDb = context.Parameters - .Where(param => newParameters.Any(newParam => newParam.Key == param.Key && newParam.Value == param.Value)) - .ToList(); + private void AttachParametersToEvent(List parametersToAdd, Event eventToUpdate) + { + _parameterService.AddParametersFromList(parametersToAdd); - var paramsToAdd = newParameters - .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) - .ToList(); + var paramsToAttach = parametersToAdd + .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) + .ToList(); - context.Parameters.AddRange(paramsToAdd); - context.SaveChanges(); + paramsToAttach.ForEach(eventToUpdate.Parameters.Add); - var paramsToAttach = newParameters - .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) - .ToList(); + context.SaveChanges(); + } - paramsToAttach.ForEach(eventToUpdate.Parameters.Add); + private void DetachParametersFromEvent(List parametersToRemove, Event eventToUpdate) + { + parametersToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); + context.SaveChanges(); - context.SaveChanges(); - } + _parameterService.RemoveParametersNotInAnyEvent(); } public void DeleteEvent(int eventId) @@ -167,9 +156,9 @@ public void DeleteEvent(int eventId) } RemoveTags(eventToDelete, eventId); - RemoveParameters(eventToDelete, eventId); - RemoveComments(eventToDelete); - RemoveParticipants(eventToDelete); + RemoveParametersFromEventCollection(eventToDelete, eventId); + RemoveCommentsFromEventCollection(eventToDelete); + RemoveParticipantsFromEventCollection(eventToDelete); context.Events.Remove(eventToDelete); context.SaveChanges(); @@ -190,7 +179,7 @@ private void RemoveTags(Event eventToDelete, int eventId) } } - private void RemoveParameters(Event eventToDelete, int eventId) + private void RemoveParametersFromEventCollection(Event eventToDelete, int eventId) { foreach (var parameter in eventToDelete.Parameters.ToList()) { @@ -203,17 +192,17 @@ private void RemoveParameters(Event eventToDelete, int eventId) } } - private void RemoveComments(Event eventToDelete) + private void RemoveCommentsFromEventCollection(Event eventToDelete) { context.Comments.RemoveRange(eventToDelete.Comments); } - private void RemoveParticipants(Event eventToDelete) + private void RemoveParticipantsFromEventCollection(Event eventToDelete) { context.Participants.RemoveRange(eventToDelete.Participants); } - public EventDto? GetEventById(int eventId) + public EventDto GetEventById(int eventId) { var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Id == eventId); if (eventToReturn is null) @@ -226,7 +215,7 @@ private void RemoveParticipants(Event eventToDelete) return eventDto; } - public EventDto? GetEventBySlug(string slug) + public EventDto GetEventBySlug(string slug) { var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) @@ -242,14 +231,7 @@ private void RemoveParticipants(Event eventToDelete) public IEnumerable GetEventsByFilter(Expression> filter) { var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Where(filter).ToList(); - var eventDtos = new List(); - foreach (var eventToReturn in events) - { - var eventDto = eventToReturn.ToDto(); - - eventDtos.Add(eventDto); - } - + var eventDtos = events.ToDtoList(); return eventDtos; } @@ -257,14 +239,7 @@ public IEnumerable GetAllEvents() { var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).ToList(); - var eventDtos = new List(); - foreach (var eventToReturn in events) - { - var eventDto = eventToReturn.ToDto(); - - eventDtos.Add(eventDto); - } - + var eventDtos = events.ToDtoList(); return eventDtos; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index b2498796..db1929cb 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -11,6 +11,11 @@ public class ImageService(ReasnContext context) { public IEnumerable CreateImages(List imageDtos) { + if (!imageDtos.Any()) + { + throw new ArgumentException("No images provided"); + } + var newImages = new List(); if (imageDtos[0].ObjectType == ObjectType.User && imageDtos.Count > 1) @@ -105,7 +110,7 @@ public void UpdateImages(int objectId, List imageDtos) context.SaveChanges(); } - public void DeleteImage(int id) + public bool DeleteImageById(int id) { var image = context.Images.FirstOrDefault(r => r.Id == id); if (image is null) @@ -115,6 +120,21 @@ public void DeleteImage(int id) context.Images.Remove(image); context.SaveChanges(); + return true; + } + + public bool DeleteImageByObjectIdAndType(int objectId, ObjectType objectType) + { + var images = context.Images.Where(r => r.ObjectId == objectId && r.ObjectType == objectType).ToList(); + if (!images.Any()) + { + throw new NotFoundException("Images not found"); + } + + context.Images.RemoveRange(images); + context.SaveChanges(); + + return true; } public ImageDto GetImageById(int id) @@ -130,10 +150,10 @@ public ImageDto GetImageById(int id) return imageDto; } - public IEnumerable GetImagesByObjectId(int objectId, ObjectType objectType) + public IEnumerable GetImagesByUserId(int userId) { var images = context.Images - .Where(image => image.ObjectId == objectId && image.ObjectType == objectType) + .Where(image => image.ObjectId == userId && image.ObjectType == ObjectType.User) .ToList(); if (!images.Any()) @@ -141,7 +161,7 @@ public IEnumerable GetImagesByObjectId(int objectId, ObjectType object throw new NotFoundException("Images not found"); } - var imageDtos = images.Select(image => image.ToDto()).AsEnumerable(); + var imageDtos = images.ToDtoList().AsEnumerable(); return imageDtos; } @@ -167,7 +187,12 @@ public IEnumerable GetImagesByEventId(int eventId) .Where(image => image.ObjectType == ObjectType.Event && image.ObjectId == eventId) .ToList(); - var imageDtos = images.Select(image => image.ToDto()).AsEnumerable(); + if (!images.Any()) + { + throw new NotFoundException("Images not found"); + } + + var imageDtos = images.ToDtoList().AsEnumerable(); return imageDtos; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs index 964ee451..9d4bd7d2 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/InterestService.cs @@ -56,6 +56,21 @@ public void DeleteInterest(int id) context.SaveChanges(); } + public void ForceDeleteInterest(int id) + { + var interest = context.Interests.FirstOrDefault(r => r.Id == id); + if (interest is null) + { + throw new NotFoundException("Interest not found"); + } + + var userInterests = context.UserInterests.Where(r => r.InterestId == id).ToList(); + context.UserInterests.RemoveRange(userInterests); + + context.Interests.Remove(interest); + context.SaveChanges(); + } + public InterestDto GetInterestById(int interestId) { var interest = context.Interests.Find(interestId); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index cddacfb7..82de3c18 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -71,6 +71,29 @@ public void DeleteParameter(int parameterId) context.Parameters.Remove(parameter); context.SaveChanges(); } + public void RemoveParametersNotInAnyEvent() + { + var parametersNotInAnyEvent = context.Parameters + .Where(p => !context.Events.Any(e => e.Parameters.Contains(p))) + .ToList(); + + context.Parameters.RemoveRange(parametersNotInAnyEvent); + context.SaveChanges(); + } + + public void AddParametersFromList(List parametersToAdd) + { + var existingParamsInDb = context.Parameters + .Where(param => parametersToAdd.Any(newParam => newParam.Key == param.Key && newParam.Value == param.Value)) + .ToList(); + + var paramsToAdd = parametersToAdd + .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) + .ToList(); + + context.Parameters.AddRange(paramsToAdd); + context.SaveChanges(); + } public ParameterDto GetParameterById(int parameterId) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 5f829a2c..47647895 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -22,7 +22,7 @@ public TagDto CreateTag(TagDto tagDto) return tagDto; } - public TagDto UpdateTag(int tagId, TagDto tagDto, int eventId) + public TagDto UpdateTagForEvent(int tagId, TagDto tagDto, int eventId) { using (var scope = new TransactionScope()) { @@ -67,6 +67,35 @@ public TagDto UpdateTag(int tagId, TagDto tagDto, int eventId) return tagDto; } } + public TagDto UpdateTag(int tagId, TagDto tagDto) + { + var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); + + if (tag is null) + { + throw new NotFoundException("Tag not found"); + } + + tag.Name = tagDto.Name; + context.Tags.Update(tag); + context.SaveChanges(); + + return tagDto; + } + + public void AddTagsFromList(List tagsToAdd) + { + var existingTagsInDb = context.Tags + .Where(tag => tagsToAdd.Any(newTag => newTag.Name == tag.Name)) + .ToList(); + + var tagsToAddToDb = tagsToAdd + .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) + .ToList(); + + context.Tags.AddRange(tagsToAddToDb); + context.SaveChanges(); + } public void DeleteTag(int tagId) { @@ -90,6 +119,16 @@ public void DeleteTag(int tagId) context.SaveChanges(); } + public void RemoveTagsNotInAnyEvent() + { + var tagsNotInAnyEvent = context.Tags + .Where(t => !context.Events.Any(e => e.Tags.Contains(t))) + .ToList(); + + context.Tags.RemoveRange(tagsNotInAnyEvent); + context.SaveChanges(); + } + public TagDto GetTagById(int tagId) { var tag = context.Tags.Find(tagId); From 12bbc028d5b21569870a6ccbfe6aadc72ac6c1ca Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Tue, 11 Jun 2024 19:52:43 +0200 Subject: [PATCH 45/65] Update EventService.cs --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index eb432d5a..0fb264d5 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -24,6 +24,7 @@ public EventDto CreateEvent(EventDto eventDto) newEvent.Slug = CreateSlug(eventDto, createdTime); context.Events.Add(newEvent); + context.SaveChanges(); if (eventDto.Tags != null) { @@ -242,6 +243,18 @@ public IEnumerable GetAllEvents() var eventDtos = events.ToDtoList(); return eventDtos; } + public IEnumerable GetUserEvents(string username) + { + var user = context.Users.FirstOrDefault(u => u.Username == username); + + if (user is null) + { + throw new NotFoundException("User not found"); + } + + var userEvents = context.Participants.Include(p => p.Event).Where(p => p.UserId == user.Id).Select(p => p.Event); + return userEvents.ToDtoList().AsEnumerable(); + } private string CreateSlug(EventDto eventDto, DateTime createdTime) { From 6886e31007d4ae3a89d871a2988bafd3d27bc6b3 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 15:16:20 +0200 Subject: [PATCH 46/65] changes --- .../Services/EventServicesTest.cs | 8 ++-- .../ReasnAPI/Models/Database/Event.cs | 4 +- .../ReasnAPI/Services/EventService.cs | 47 +++++-------------- .../ReasnAPI/Services/ImageService.cs | 6 +-- .../ReasnAPI/Services/ParameterService.cs | 19 ++++++++ .../ReasnAPI/ReasnAPI/Services/TagService.cs | 18 +++++++ 6 files changed, 56 insertions(+), 46 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 4f0a2d56..a9756e4f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -142,7 +142,7 @@ public void UpdateEvent_EventDoesNotExist_NullReturned() UpdatedAt =DateTime.Now }}); - var eventService = new EventService(mockContext.Object); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); Assert.ThrowsException(() => eventService.UpdateEvent(1, eventDto)); } @@ -203,7 +203,7 @@ public void GetEventById_EventExists_EventReturned() UpdatedAt =DateTime.Now }}); - var eventService = new EventService(mockContext.Object); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); var result = eventService.GetEventById(1); Assert.IsNotNull(result); @@ -248,7 +248,7 @@ public void GetEventById_EventDoesNotExist_NullReturned() }}); - var eventService = new EventService(mockContext.Object); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); Assert.ThrowsException(() => eventService.GetEventById(1)); } @@ -318,7 +318,7 @@ public void DeleteEvent_EventExists_EventDeleted() mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); - var eventService = new EventService(mockContext.Object); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); eventService.DeleteEvent(1); diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs index 80892807..fef3be28 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Database/Event.cs @@ -26,10 +26,10 @@ public partial class Event public DateTime UpdatedAt { get; set; } - public EventStatus Status { get; set; } - public string Slug { get; set; } = null!; + public EventStatus Status { get; set; } + public virtual Address Address { get; set; } = null!; public virtual ICollection Comments { get; set; } = new List(); diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 0fb264d5..2f904e38 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -8,10 +8,8 @@ using ReasnAPI.Mappers; namespace ReasnAPI.Services; -public class EventService(ReasnContext context) +public class EventService(ReasnContext context, ParameterService parameterService, TagService tagService) { - private readonly ParameterService _parameterService = new ParameterService(context); - private readonly TagService _tagService = new TagService(context); public EventDto CreateEvent(EventDto eventDto) { @@ -38,7 +36,6 @@ public EventDto CreateEvent(EventDto eventDto) AttachParametersToEvent(newParameters, newEvent); } - context.SaveChanges(); scope.Complete(); } @@ -105,7 +102,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) private void AttatchTagsToEvent(List tagsToAdd, Event eventToUpdate) { - _tagService.AddTagsFromList(tagsToAdd); + tagService.AddTagsFromList(tagsToAdd); var tagsToAttach = tagsToAdd .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) @@ -121,12 +118,12 @@ private void DetachTagsFromEvent(List tagsToRemove, Event eventToUpdate) tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); context.SaveChanges(); - _tagService.RemoveTagsNotInAnyEvent(); + tagService.RemoveTagsNotInAnyEvent(); } private void AttachParametersToEvent(List parametersToAdd, Event eventToUpdate) { - _parameterService.AddParametersFromList(parametersToAdd); + parameterService.AddParametersFromList(parametersToAdd); var paramsToAttach = parametersToAdd .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) @@ -142,7 +139,7 @@ private void DetachParametersFromEvent(List parametersToRemove, Event parametersToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); context.SaveChanges(); - _parameterService.RemoveParametersNotInAnyEvent(); + parameterService.RemoveParametersNotInAnyEvent(); } public void DeleteEvent(int eventId) @@ -156,8 +153,12 @@ public void DeleteEvent(int eventId) throw new NotFoundException("Event not found"); } - RemoveTags(eventToDelete, eventId); - RemoveParametersFromEventCollection(eventToDelete, eventId); + var parametersToRemove = eventToDelete.Parameters.ToList(); + var tagsToRemove = eventToDelete.Tags.ToList(); + + DetachParametersFromEvent(parametersToRemove, eventToDelete); + DetachTagsFromEvent(tagsToRemove, eventToDelete); + RemoveCommentsFromEventCollection(eventToDelete); RemoveParticipantsFromEventCollection(eventToDelete); @@ -167,32 +168,6 @@ public void DeleteEvent(int eventId) } } - private void RemoveTags(Event eventToDelete, int eventId) - { - foreach (var tag in eventToDelete.Tags.ToList()) - { - eventToDelete.Tags.Remove(tag); // remove the association first - // Check if the tag is associated with any other event - if (!context.Events.Any(e => e.Tags.Any(t => t.Name == tag.Name) && e.Id != eventId) && !context.Events.Any(e => e.Tags.Contains(tag))) - { - context.Tags.Remove(tag); // then remove the tag - } - } - } - - private void RemoveParametersFromEventCollection(Event eventToDelete, int eventId) - { - foreach (var parameter in eventToDelete.Parameters.ToList()) - { - eventToDelete.Parameters.Remove(parameter); // remove the association first - // Check if the parameter is associated with any other event - if (!context.Events.Any(e => e.Parameters.Any(p => p.Key == parameter.Key && p.Value == parameter.Value) && e.Id != eventId) && !context.Events.Any(e => e.Parameters.Contains(parameter))) - { - context.Parameters.Remove(parameter); // then remove the parameter - } - } - } - private void RemoveCommentsFromEventCollection(Event eventToDelete) { context.Comments.RemoveRange(eventToDelete.Comments); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index db1929cb..c6d4fb88 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -110,7 +110,7 @@ public void UpdateImages(int objectId, List imageDtos) context.SaveChanges(); } - public bool DeleteImageById(int id) + public void DeleteImageById(int id) { var image = context.Images.FirstOrDefault(r => r.Id == id); if (image is null) @@ -120,10 +120,9 @@ public bool DeleteImageById(int id) context.Images.Remove(image); context.SaveChanges(); - return true; } - public bool DeleteImageByObjectIdAndType(int objectId, ObjectType objectType) + public void DeleteImageByObjectIdAndType(int objectId, ObjectType objectType) { var images = context.Images.Where(r => r.ObjectId == objectId && r.ObjectType == objectType).ToList(); if (!images.Any()) @@ -134,7 +133,6 @@ public bool DeleteImageByObjectIdAndType(int objectId, ObjectType objectType) context.Images.RemoveRange(images); context.SaveChanges(); - return true; } public ImageDto GetImageById(int id) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 82de3c18..17f56a78 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -71,6 +71,25 @@ public void DeleteParameter(int parameterId) context.Parameters.Remove(parameter); context.SaveChanges(); } + + public void ForceDeleteParameter(int parameterId) + { + var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + if (parameter is null) + { + throw new NotFoundException("Parameter not found"); + } + + var eventsWithParameters = context.Events.Include(e => e.Parameters).ToList(); + foreach (var eventWithParameters in eventsWithParameters) + { + eventWithParameters.Parameters.Remove(parameter); + } + + context.Parameters.Remove(parameter); + context.SaveChanges(); + } + public void RemoveParametersNotInAnyEvent() { var parametersNotInAnyEvent = context.Parameters diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 47647895..d503d17c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -119,6 +119,24 @@ public void DeleteTag(int tagId) context.SaveChanges(); } + public void ForceDeleteTag(int tagId) + { + var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); + if (tag is null) + { + throw new NotFoundException("Tag not found"); + } + + var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); + foreach (var eventWithTags in eventsWithTags) + { + eventWithTags.Tags.Remove(tag); + } + + context.Tags.Remove(tag); + context.SaveChanges(); + } + public void RemoveTagsNotInAnyEvent() { var tagsNotInAnyEvent = context.Tags From c343f40908eb0bc6f0ad618f48691695158bb820 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 15:19:39 +0200 Subject: [PATCH 47/65] Update EventServicesTest.cs --- Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index a9756e4f..b54a31b7 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -80,7 +80,7 @@ public void UpdateEvent_EventExists_EventUpdated() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); - var eventService = new EventService(mockContext.Object); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); var result = eventService.UpdateEvent(1,eventDto); Assert.AreEqual("name1",result.Name); @@ -322,7 +322,7 @@ public void DeleteEvent_EventExists_EventDeleted() eventService.DeleteEvent(1); - mockContext.Verify(c => c.SaveChanges(), Times.Once); + mockContext.Verify(c => c.SaveChanges(), Times.AtLeastOnce); } } From 55b9e7d6e8fe7636ae5e3c44f0490f0e51030712 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 16:23:46 +0200 Subject: [PATCH 48/65] updates --- .../Services/TagServiceTests.cs | 45 ------------------- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 45 ------------------- 2 files changed, 90 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 88d85028..7f6a2cdd 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -81,51 +81,6 @@ public void GetTagById_TagDoesNotExist_NullReturned() Assert.ThrowsException(() => tagService.GetTagById(1)); } - [TestMethod] - public void UpdateTag_TagExists_TagUpdated() - { - // Arrange - var tagDto = new TagDto - { - Name = "TestTag1" - }; - var tag = new Tag { Id = 1, Name = "TestTag" }; - var eventId = 1; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { tag }); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId, Tags = new List { tag } } }); - - var tagService = new TagService(mockContext.Object); - - // Act - var result = tagService.UpdateTagForEvent(1, tagDto, eventId); - - // Assert - Assert.IsNotNull(result); - Assert.AreEqual(tagDto.Name, result.Name); - Assert.AreEqual(tagDto.Name, tag.Name); // Verify the tag was updated - mockContext.Verify(c => c.SaveChanges(), Times.Once); // Ensure SaveChanges was called - } - - [TestMethod] - public void UpdateTag_TagDoesNotExist_NullReturned() - { - var tagDto = new TagDto - { - Name = "TestTag1" - }; - var eventId = 1; - - var mockContext = new Mock(); - mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List { new Event { Id = eventId } }); - var tagService = new TagService(mockContext.Object); - - Assert.ThrowsException(() => tagService.UpdateTagForEvent(1, tagDto, eventId)); - mockContext.Verify(c => c.SaveChanges(), Times.Never); // Ensure SaveChanges was never called - } - [TestMethod] public void DeleteTag_TagExists_TagDeleted() { diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index d503d17c..3bbe5c98 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -22,51 +22,6 @@ public TagDto CreateTag(TagDto tagDto) return tagDto; } - public TagDto UpdateTagForEvent(int tagId, TagDto tagDto, int eventId) - { - using (var scope = new TransactionScope()) - { - var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); - - if (tag is null) - { - throw new NotFoundException("Tag not found"); - } - - var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); - - var eventTags = eventsWithTags.Where(e => e.Tags.Any(t => t.Id == tagId)).ToList(); - var eventToUpdate = eventsWithTags.FirstOrDefault(e => e.Id == eventId); - - if (eventTags.Count > 1 || (eventTags.Count == 1 && eventTags[0].Id != eventId)) - { - // Create new tag, associate it with the event, and remove the old association - var newTag = tagDto.ToEntity(); - context.Tags.Add(newTag); - context.SaveChanges(); - - if (eventToUpdate != null) - { - eventToUpdate.Tags.Remove(tag); - eventToUpdate.Tags.Add(newTag); - context.Events.Update(eventToUpdate); - } - } - else if (eventTags.Count == 1 && eventTags[0].Id == eventId) - { - tag.Name = tagDto.Name; - context.Tags.Update(tag); - } - else - { - throw new NotFoundException("Tag not found"); - } - - context.SaveChanges(); - scope.Complete(); - return tagDto; - } - } public TagDto UpdateTag(int tagId, TagDto tagDto) { var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); From 912344c315131241f456398d89645d01b750a64b Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 19:03:20 +0200 Subject: [PATCH 49/65] Update EventService.cs --- .../ReasnAPI/Services/EventService.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 2f904e38..3f9510a3 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -204,6 +204,33 @@ public EventDto GetEventBySlug(string slug) return eventDto; } + public IEnumerable GetEventParticipantsBySlug(string slug) + { + var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); + if (eventToReturn is null) + { + throw new NotFoundException("Event not found"); + } + + var participantsDto = eventToReturn.Participants.Select(p => p.ToDto()); + + return participantsDto; + } + + public IEnumerable GetEventCommentsBySlug(string slug) + { + var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Comments) + .Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); + if (eventToReturn is null) + { + throw new NotFoundException("Event not found"); + } + + var commentDtos = eventToReturn.Comments.Select(p => p.ToDto()); + + return commentDtos; + } + public IEnumerable GetEventsByFilter(Expression> filter) { var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Where(filter).ToList(); From e747b3d0844ea9dacb8fd06a58a789645fbb09f5 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 19:24:28 +0200 Subject: [PATCH 50/65] Update EventService.cs --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 3f9510a3..a4b5ab04 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -191,7 +191,7 @@ public EventDto GetEventById(int eventId) return eventDto; } - public EventDto GetEventBySlug(string slug) + public Event GetEventBySlug(string slug) { var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) @@ -199,9 +199,8 @@ public EventDto GetEventBySlug(string slug) throw new NotFoundException("Event not found"); } - var eventDto = eventToReturn.ToDto(); - return eventDto; + return eventToReturn; } public IEnumerable GetEventParticipantsBySlug(string slug) From a79b31fa0e10d778808d2124b506cf71756c147a Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 20:05:30 +0200 Subject: [PATCH 51/65] Update ImageService.cs --- .../ReasnAPI/Services/ImageService.cs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index c6d4fb88..fbe93b77 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -108,6 +108,25 @@ public void UpdateImages(int objectId, List imageDtos) } context.SaveChanges(); + } + + public void UpdateImage(int objectId, ImageDto imageDto) + { + if (imageDto is null) + { + throw new ArgumentException("No image provided"); + } + + var image = context.Images.Where(i => i.ObjectType == imageDto.ObjectType).FirstOrDefault(r => r.Id == objectId); + if (image is null) + { + throw new NotFoundException("Image not found"); + } + + image.ImageData = imageDto.ImageData; + context.Images.Update(image); + context.SaveChanges(); + } public void DeleteImageById(int id) @@ -118,6 +137,29 @@ public void DeleteImageById(int id) throw new NotFoundException("Image not found"); } + context.Images.Remove(image); + context.SaveChanges(); + } + + public void DeleteImageRelatedToEvent(int id, string slug) + { + var @event = context.Events.FirstOrDefault(r => r.Id == id && r.Slug == slug); + if (@event is null) + { + throw new NotFoundException("Event not found"); + } + + if (@event.Id != id) + { + throw new NotFoundException("Image is not related with this event"); + } + + var image = context.Images.FirstOrDefault(r => r.Id == id); + if (image is null) + { + throw new NotFoundException("Image not found"); + } + context.Images.Remove(image); context.SaveChanges(); } @@ -132,7 +174,6 @@ public void DeleteImageByObjectIdAndType(int objectId, ObjectType objectType) context.Images.RemoveRange(images); context.SaveChanges(); - } public ImageDto GetImageById(int id) From 2f4128c01ab45ccc573d9a982a0a428fd51d91d0 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 20:12:54 +0200 Subject: [PATCH 52/65] Updates --- .../Services/ImageServiceTests.cs | 6 +++--- .../ReasnAPI/Services/ImageService.cs | 19 +++---------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 0de2de08..84977285 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -136,7 +136,7 @@ public void UpdateImages_UserType_ImageUpdated() var imageService = new ImageService(mockContext.Object); - imageService.UpdateImages(1, new List { imageDto }); + imageService.UpdateImage(1, imageDto ); mockContext.Verify(c => c.SaveChanges(), Times.Once); } @@ -166,7 +166,7 @@ public void UpdateImages_EventType_ImagesUpdated() var imageService = new ImageService(mockContext.Object); - imageService.UpdateImages(1, imageDtos); + imageService.UpdateImagesForEvent(1, imageDtos); mockContext.Verify(c => c.SaveChanges(), Times.Once); } @@ -186,7 +186,7 @@ public void UpdateImages_ImageDoesNotExist_ExceptionThrown() var imageService = new ImageService(mockContext.Object); - Assert.ThrowsException(() => imageService.UpdateImages(1, new List { imageDto })); + Assert.ThrowsException(() => imageService.UpdateImagesForEvent(1, new List { imageDto })); } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index fbe93b77..9cb50266 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -55,7 +55,7 @@ public IEnumerable CreateImages(List imageDtos) return imageDtos.AsEnumerable(); } - public void UpdateImages(int objectId, List imageDtos) + public void UpdateImagesForEvent(int objectId, List imageDtos) { if (!imageDtos.Any()) { @@ -66,20 +66,7 @@ public void UpdateImages(int objectId, List imageDtos) if (objectType == ObjectType.User) { - if (imageDtos.Count != 1) - { - throw new ArgumentException("For User type, only one image can be updated"); - } - - var image = context.Images.FirstOrDefault(r => r.ObjectId == objectId && r.ObjectType == ObjectType.User); - if (image is null) - { - throw new NotFoundException("Image not found"); - } - - image.ImageData = imageDtos[0].ImageData; - - context.Images.Update(image); + throw new ArgumentException("For User type, use UpdateImage"); } else if (objectType == ObjectType.Event) { @@ -117,7 +104,7 @@ public void UpdateImage(int objectId, ImageDto imageDto) throw new ArgumentException("No image provided"); } - var image = context.Images.Where(i => i.ObjectType == imageDto.ObjectType).FirstOrDefault(r => r.Id == objectId); + var image = context.Images.Where(i => i.ObjectType == imageDto.ObjectType).FirstOrDefault(r => r.ObjectId == objectId); if (image is null) { throw new NotFoundException("Image not found"); From 450849b96884d8f0d1e1c6b74d48cb596a72758e Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 20:51:04 +0200 Subject: [PATCH 53/65] Update EventMapper.cs --- Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs b/Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs index a95ddca6..0cf4cea7 100644 --- a/Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs +++ b/Server/ReasnAPI/ReasnAPI/Mappers/EventMapper.cs @@ -7,13 +7,9 @@ public static class EventMapper { public static EventDto ToDto(this Event eventToMap) { - var tags = eventToMap.Tags - .Select(t => new TagDto { Name = t.Name }) - .ToList(); + var tags = eventToMap.Tags.ToDtoList(); - var parameters = eventToMap.Parameters - .Select(p => new ParameterDto { Key = p.Key, Value = p.Value }) - .ToList(); + var parameters = eventToMap.Parameters.ToDtoList(); return new EventDto { From f51885ec974cc7b21f69ad382f9b86a7e8b2d228 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 22:50:36 +0200 Subject: [PATCH 54/65] changes --- .../ReasnAPI/Services/EventService.cs | 4 ++-- .../ReasnAPI/Services/ImageService.cs | 8 +++---- .../ReasnAPI/Services/ParameterService.cs | 16 +++++++++----- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 22 ++++++++++++++----- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index a4b5ab04..261467ee 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -205,7 +205,7 @@ public Event GetEventBySlug(string slug) public IEnumerable GetEventParticipantsBySlug(string slug) { - var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); + var eventToReturn = context.Events.Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) { throw new NotFoundException("Event not found"); @@ -218,7 +218,7 @@ public IEnumerable GetEventParticipantsBySlug(string slug) public IEnumerable GetEventCommentsBySlug(string slug) { - var eventToReturn = context.Events.Include(e => e.Tags).Include(e => e.Parameters).Include(e => e.Comments) + var eventToReturn = context.Events.Include(e => e.Comments) .Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 9cb50266..5d1d769a 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -97,15 +97,15 @@ public void UpdateImagesForEvent(int objectId, List imageDtos) context.SaveChanges(); } - public void UpdateImage(int objectId, ImageDto imageDto) + public void UpdateImageForUser(int userId, ImageDto imageDto) { if (imageDto is null) { throw new ArgumentException("No image provided"); } - var image = context.Images.Where(i => i.ObjectType == imageDto.ObjectType).FirstOrDefault(r => r.ObjectId == objectId); - if (image is null) + var image = context.Images.FirstOrDefault(i => i.ObjectType == ObjectType.User && i.ObjectId == userId); + if (image == null) { throw new NotFoundException("Image not found"); } @@ -130,7 +130,7 @@ public void DeleteImageById(int id) public void DeleteImageRelatedToEvent(int id, string slug) { - var @event = context.Events.FirstOrDefault(r => r.Id == id && r.Slug == slug); + var @event = context.Events.FirstOrDefault(r => r.Slug == slug); if (@event is null) { throw new NotFoundException("Event not found"); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 17f56a78..9aaece6b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -72,18 +72,22 @@ public void DeleteParameter(int parameterId) context.SaveChanges(); } - public void ForceDeleteParameter(int parameterId) + public void ForceDeleteParameter(ParameterDto parameterDto) { - var parameter = context.Parameters.FirstOrDefault(r => r.Id == parameterId); + var parameter = context.Parameters.FirstOrDefault(r => r.Key == parameterDto.Key && r.Value == parameterDto.Value); if (parameter is null) { throw new NotFoundException("Parameter not found"); - } + } + + var eventsWithParameter = context.Events + .Where(e => e.Parameters.Any(p => p.Key == parameterDto.Key && p.Value == parameterDto.Value)) + .Include(e => e.Parameters) + .ToList(); - var eventsWithParameters = context.Events.Include(e => e.Parameters).ToList(); - foreach (var eventWithParameters in eventsWithParameters) + foreach (var eventWithParameter in eventsWithParameter) { - eventWithParameters.Parameters.Remove(parameter); + eventWithParameter.Parameters.Remove(parameter); } context.Parameters.Remove(parameter); diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 3bbe5c98..b2bf30b8 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -74,20 +74,30 @@ public void DeleteTag(int tagId) context.SaveChanges(); } - public void ForceDeleteTag(int tagId) + public void ForceDeleteTag(TagDto tagDto) { - var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); + if (tagDto is null) + { + throw new ArgumentException("No tag provided"); + } + + var tag = context.Tags.FirstOrDefault(r => r.Name == tagDto.Name); if (tag is null) { throw new NotFoundException("Tag not found"); - } + } + + var eventsWithTags = context.Events + .Where(e => e.Tags.Any(p => p.Name == tagDto.Name)) + .Include(e => e.Tags) + .ToList(); - var eventsWithTags = context.Events.Include(e => e.Tags).ToList(); - foreach (var eventWithTags in eventsWithTags) + foreach (var eventWithTag in eventsWithTags) { - eventWithTags.Tags.Remove(tag); + eventWithTag.Tags.Remove(tag); } + context.Tags.Remove(tag); context.SaveChanges(); } From 631d8b747213b9bf6782d1409a3340617cc8eea7 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Wed, 12 Jun 2024 22:51:13 +0200 Subject: [PATCH 55/65] Update ImageServiceTests.cs --- Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index 84977285..b1b0ecff 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -136,7 +136,7 @@ public void UpdateImages_UserType_ImageUpdated() var imageService = new ImageService(mockContext.Object); - imageService.UpdateImage(1, imageDto ); + imageService.UpdateImageForUser(1, imageDto ); mockContext.Verify(c => c.SaveChanges(), Times.Once); } From 9bb1ca6ccc2980d65ad4e0e18074faf936a2e5b3 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 13 Jun 2024 01:21:10 +0200 Subject: [PATCH 56/65] changes --- .../ReasnAPI/ReasnAPI/Services/EventService.cs | 2 +- .../ReasnAPI/ReasnAPI/Services/ImageService.cs | 16 ++++++++-------- Server/ReasnAPI/ReasnAPI/Services/TagService.cs | 7 ++++++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 261467ee..a086f868 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -219,7 +219,7 @@ public IEnumerable GetEventParticipantsBySlug(string slug) public IEnumerable GetEventCommentsBySlug(string slug) { var eventToReturn = context.Events.Include(e => e.Comments) - .Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); + .FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) { throw new NotFoundException("Event not found"); diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index 5d1d769a..b5b651b4 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -105,7 +105,7 @@ public void UpdateImageForUser(int userId, ImageDto imageDto) } var image = context.Images.FirstOrDefault(i => i.ObjectType == ObjectType.User && i.ObjectId == userId); - if (image == null) + if (image is null) { throw new NotFoundException("Image not found"); } @@ -130,23 +130,23 @@ public void DeleteImageById(int id) public void DeleteImageRelatedToEvent(int id, string slug) { - var @event = context.Events.FirstOrDefault(r => r.Slug == slug); - if (@event is null) + var relatedEvent = context.Events.FirstOrDefault(r => r.Slug == slug); + if (relatedEvent is null) { throw new NotFoundException("Event not found"); } - if (@event.Id != id) - { - throw new NotFoundException("Image is not related with this event"); - } - var image = context.Images.FirstOrDefault(r => r.Id == id); if (image is null) { throw new NotFoundException("Image not found"); } + if (image.ObjectId != relatedEvent.Id || image.ObjectType != ObjectType.Event) + { + throw new NotFoundException("This image is not related with this event"); + } + context.Images.Remove(image); context.SaveChanges(); } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index b2bf30b8..f9f13f89 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -40,6 +40,11 @@ public TagDto UpdateTag(int tagId, TagDto tagDto) public void AddTagsFromList(List tagsToAdd) { + if (tagsToAdd is null) + { + throw new ArgumentException("No tag provided"); + } + var existingTagsInDb = context.Tags .Where(tag => tagsToAdd.Any(newTag => newTag.Name == tag.Name)) .ToList(); @@ -56,7 +61,7 @@ public void DeleteTag(int tagId) { var tag = context.Tags.FirstOrDefault(r => r.Id == tagId); - if (tag == null) + if (tag is null) { throw new NotFoundException("Tag not found"); } From 3c01c4f2333fb9e7864aeea4c4fe06a4a826c050 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 13 Jun 2024 01:31:39 +0200 Subject: [PATCH 57/65] Formating --- .../Services/EventServicesTest.cs | 98 +++++++++---------- .../Services/ImageServiceTests.cs | 2 +- .../Services/InterestServiceTests.cs | 14 +-- .../Services/ParameterServiceTests.cs | 30 +++--- .../Services/TagServiceTests.cs | 24 ++--- .../ReasnAPI/Models/Database/ReasnContext.cs | 2 +- .../ReasnAPI/Services/EventService.cs | 2 +- .../ReasnAPI/Services/ImageService.cs | 6 +- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 6 +- 9 files changed, 92 insertions(+), 92 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index b54a31b7..9b9da548 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -36,7 +36,7 @@ public void UpdateEvent_EventExists_EventUpdated() }; var mockContext = new Mock(); - mockContext.Setup(c => c.Events).ReturnsDbSet(new List{ + mockContext.Setup(c => c.Events).ReturnsDbSet(new List{ new Event() { Id = 1, @@ -51,40 +51,40 @@ public void UpdateEvent_EventExists_EventUpdated() Status = EventStatus.Completed, }}); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { - Id = 1, - City = "city", - Country = "country", - State = "state", - Street = "street", + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", ZipCode = "test123" }}); - mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ new User() { - Id = 1, - Name = "test", - Email = "test@wp.pl", - AddressId = 1, + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, CreatedAt = DateTime.Now, - IsActive = true, Role = UserRole.Admin, + IsActive = true, Role = UserRole.Admin, Password ="test123", - Phone = "123123123", - Surname ="test", - Username ="test", + Phone = "123123123", + Surname ="test", + Username ="test", UpdatedAt =DateTime.Now }}); - + mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); - var result = eventService.UpdateEvent(1,eventDto); - Assert.AreEqual("name1",result.Name); - Assert.AreEqual("description2",result.Description); + var result = eventService.UpdateEvent(1, eventDto); + Assert.AreEqual("name1", result.Name); + Assert.AreEqual("description2", result.Description); Assert.AreEqual(1, result.Tags.Count); } @@ -114,34 +114,34 @@ public void UpdateEvent_EventDoesNotExist_NullReturned() var mockContext = new Mock(); mockContext.Setup(c => c.Events).ReturnsDbSet(new List()); mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); - + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { Id = 1, City = "city", - Country = "country", - State = "state", - Street = "street", + Country = "country", + State = "state", + Street = "street", ZipCode = "test123" }}); - mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ new User() { - Id = 1, - Name = "test", - Email = "test@wp.pl", - AddressId = 1, + Id = 1, + Name = "test", + Email = "test@wp.pl", + AddressId = 1, CreatedAt = DateTime.Now, - IsActive = true, - Role = UserRole.Admin, + IsActive = true, + Role = UserRole.Admin, Password ="test123", Phone = "123123123", Surname ="test", - Username ="test", + Username ="test", UpdatedAt =DateTime.Now }}); - + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); Assert.ThrowsException(() => eventService.UpdateEvent(1, eventDto)); @@ -171,30 +171,30 @@ public void GetEventById_EventExists_EventReturned() { new Tag() { - Id = 1, + Id = 1, Name = "name" } }); - + mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List
{ new Address() { - Id = 1, - City = "city", - Country = "country", + Id = 1, + City = "city", + Country = "country", State = "state", - Street = "street", + Street = "street", ZipCode = "test123" }}); - mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ + mockContext.Setup(c => c.Users).ReturnsDbSet(new List{ new User() { - Id = 1, + Id = 1, Name = "test", Email = "test@wp.pl", - AddressId = 1, + AddressId = 1, CreatedAt = DateTime.Now, - IsActive = true, + IsActive = true, Role = UserRole.User, Password ="test123", Phone = "123123123", @@ -207,7 +207,7 @@ public void GetEventById_EventExists_EventReturned() var result = eventService.GetEventById(1); Assert.IsNotNull(result); - + } [TestMethod] @@ -222,11 +222,11 @@ public void GetEventById_EventDoesNotExist_NullReturned() { new Address() { - Id = 1, - City = "city", - Country = "country", - State = "state", - Street = "street", + Id = 1, + City = "city", + Country = "country", + State = "state", + Street = "street", ZipCode = "test123" }}); mockContext.Setup(c => c.Users).ReturnsDbSet(new List diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs index b1b0ecff..746f9b9f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs @@ -136,7 +136,7 @@ public void UpdateImages_UserType_ImageUpdated() var imageService = new ImageService(mockContext.Object); - imageService.UpdateImageForUser(1, imageDto ); + imageService.UpdateImageForUser(1, imageDto); mockContext.Verify(c => c.SaveChanges(), Times.Once); } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs index ecc24fe6..ba92cb9d 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs @@ -22,9 +22,9 @@ public void CreateInterest_InterestDoesNotExist_InterestCreated() mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); var interestService = new InterestService(mockContext.Object); - + var result = interestService.CreateInterest(interestDto); - + Assert.AreEqual(interestDto.Name, result.Name); } @@ -40,7 +40,7 @@ public void CreateInterest_InterestExists_InterestNotCreated() mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Name = "TestInterest" } }); var interestService = new InterestService(mockContext.Object); - + Assert.ThrowsException(() => interestService.CreateInterest(interestDto)); } @@ -93,9 +93,9 @@ public void UpdateInterest_InterestExists_InterestUpdated() mockContext.Setup(c => c.Interests).ReturnsDbSet(new List { new Interest { Id = 1, Name = "TestInterest" } }); var interestService = new InterestService(mockContext.Object); - + var result = interestService.UpdateInterest(1, interestDto); - + Assert.AreEqual("TestInterest1", result.Name); } @@ -111,7 +111,7 @@ public void UpdateInterest_InterestDoesNotExist_NullReturned() mockContext.Setup(c => c.Interests).ReturnsDbSet(new List()); var interestService = new InterestService(mockContext.Object); - + Assert.ThrowsException(() => interestService.UpdateInterest(1, interestDto)); } @@ -175,7 +175,7 @@ public void GetInterestsByFilter_InterestExists_InterestReturned() var result = interestService.GetInterestsByFilter(i => i.Name == "TestInterest").ToList(); Assert.AreEqual(1, result.Count()); - + } [TestMethod] diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs index 733d9971..a7ce743f 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/ParameterServiceTests.cs @@ -23,9 +23,9 @@ public void CreateParameter_ParameterDoesNotExist_ParameterCreated() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); - + var result = parameterService.CreateParameter(parameterDto); - + Assert.AreEqual(parameterDto.Value, result.Value); } @@ -42,7 +42,7 @@ public void CreateParameter_ParameterExists_ParameterNotCreated() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Key = "TestKey", Value = "TestValue" } }); var parameterService = new ParameterService(mockContext.Object); - + Assert.ThrowsException(() => parameterService.CreateParameter(parameterDto)); } @@ -123,7 +123,7 @@ public void GetParameterById_ParameterDoesNotExist_NullReturned() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); - + Assert.ThrowsException(() => parameterService.GetParameterById(1)); } @@ -156,9 +156,9 @@ public void DeleteParameter_ParameterDoesNotExist_NothingDeleted() var mockContext = new Mock(); mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); - + Assert.ThrowsException(() => parameterService.DeleteParameter(1)); - + mockContext.Verify(c => c.SaveChanges(), Times.Never); } @@ -169,9 +169,9 @@ public void GetParameterByFilter_ParameterExists_ParameterReturned() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); var parameterService = new ParameterService(mockContext.Object); - + var result = parameterService.GetParametersByFilter(p => p.Key == "TestKey").ToList(); - + Assert.AreEqual(1, result.Count()); } @@ -182,9 +182,9 @@ public void GetParameterByFilter_ParameterDoesNotExist_NothingReturned() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); - + var result = parameterService.GetParametersByFilter(p => p.Key == "TestKey").ToList(); - + Assert.AreEqual(0, result.Count()); } @@ -195,9 +195,9 @@ public void GetAllParameters_ParameterExists_ParameterReturned() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List { new Parameter { Id = 1, Key = "TestKey", Value = "TestValue" } }); var parameterService = new ParameterService(mockContext.Object); - + var result = parameterService.GetAllParameters().ToList(); - + Assert.AreEqual(1, result.Count()); } @@ -208,11 +208,11 @@ public void GetAllParameters_ParameterDoesNotExist_NothingReturned() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); var parameterService = new ParameterService(mockContext.Object); - + var result = parameterService.GetAllParameters().ToList(); - + Assert.AreEqual(0, result.Count()); } - + } } diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs index 7f6a2cdd..f5d09b50 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/TagServiceTests.cs @@ -22,9 +22,9 @@ public void CreateTag_TagDoesNotExist_TagCreated() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); - + var result = tagService.CreateTag(tagDto); - + Assert.AreEqual(tagDto.Name, result.Name); } @@ -40,7 +40,7 @@ public void CreateTag_TagExists_TagNotCreated() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Name = "TestTag" } }); var tagService = new TagService(mockContext.Object); - + Assert.ThrowsException(() => tagService.CreateTag(tagDto)); } @@ -51,9 +51,9 @@ public void GetAllTags_TagExists_TagsReturned() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Name = "TestTag" } }); var tagService = new TagService(mockContext.Object); - + var result = tagService.GetAllTags().ToList(); - + Assert.AreEqual(1, result.Count); } @@ -64,9 +64,9 @@ public void GetAllTags_TagNotExists_EmptyListReturned() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); - + var result = tagService.GetAllTags().ToList(); - + Assert.AreEqual(0, result.Count); } @@ -77,7 +77,7 @@ public void GetTagById_TagDoesNotExist_NullReturned() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List()); var tagService = new TagService(mockContext.Object); - + Assert.ThrowsException(() => tagService.GetTagById(1)); } @@ -138,9 +138,9 @@ public void GetTagsByFilter_TagExist_TagsReturned() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); var tagService = new TagService(mockContext.Object); - + var result = tagService.GetTagsByFilter(t => t.Name == "TestTag").ToList(); - + Assert.AreEqual(1, result.Count); } @@ -151,9 +151,9 @@ public void GetTagsByFilter_TagsNotExist_TagsNotReturned() mockContext.Setup(c => c.Tags).ReturnsDbSet(new List { new Tag { Id = 1, Name = "TestTag" } }); var tagService = new TagService(mockContext.Object); - + var result = tagService.GetTagsByFilter(t => t.Name == "TestTag1").ToList(); - + Assert.AreEqual(0, result.Count); } diff --git a/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs b/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs index f24cda17..aa85b86b 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/Database/ReasnContext.cs @@ -38,7 +38,7 @@ public ReasnContext(DbContextOptions options) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - + if (!optionsBuilder.IsConfigured) { optionsBuilder.UseNpgsql("name=ConnectionStrings:DefaultValue"); diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index a086f868..aff8dc5b 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -35,7 +35,7 @@ public EventDto CreateEvent(EventDto eventDto) var newParameters = eventDto.Parameters.ToEntityList(); AttachParametersToEvent(newParameters, newEvent); } - + context.SaveChanges(); scope.Complete(); } diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index b5b651b4..cdb3672c 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -32,7 +32,7 @@ public IEnumerable CreateImages(List imageDtos) } var newImage = imageDto.ToEntity(); - + newImages.Add(newImage); } @@ -48,7 +48,7 @@ public IEnumerable CreateImages(List imageDtos) { context.Images.AddRange(newImages); } - + context.SaveChanges(); } @@ -172,7 +172,7 @@ public ImageDto GetImageById(int id) } var imageDto = image.ToDto(); - + return imageDto; } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index f9f13f89..6f9f9f55 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -7,7 +7,7 @@ using ReasnAPI.Mappers; namespace ReasnAPI.Services; -public class TagService (ReasnContext context) +public class TagService(ReasnContext context) { public TagDto CreateTag(TagDto tagDto) { @@ -70,7 +70,7 @@ public void DeleteTag(int tagId) var isTagAssociatedWithEvent = eventsWithTags.Any(e => e.Tags.Any(t => t.Id == tagId)); - if (isTagAssociatedWithEvent) + if (isTagAssociatedWithEvent) { throw new BadRequestException("Tag is associated with an event"); } @@ -120,7 +120,7 @@ public void RemoveTagsNotInAnyEvent() public TagDto GetTagById(int tagId) { var tag = context.Tags.Find(tagId); - if(tag is null) + if (tag is null) { throw new NotFoundException("Tag not found"); } From ecbdf3f92a14ad5f0f4bef89302bc13c5b3adb1a Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 13 Jun 2024 13:37:08 +0200 Subject: [PATCH 58/65] Update EventService.cs --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index aff8dc5b..8068b102 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -60,7 +60,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) eventToUpdate.OrganizerId = eventDto.OrganizerId; eventToUpdate.StartAt = eventDto.StartAt; eventToUpdate.EndAt = eventDto.EndAt; - eventToUpdate.UpdatedAt = DateTime.Now; + eventToUpdate.UpdatedAt = DateTime.UtcNow; eventToUpdate.Status = eventDto.Status; if (eventDto.Tags == null) From e74a0d20ba3721925a194d531fb71fd6c585110e Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Thu, 13 Jun 2024 13:55:01 +0200 Subject: [PATCH 59/65] Update EventService.cs --- .../ReasnAPI/Services/EventService.cs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 8068b102..2e90bd9f 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -19,7 +19,7 @@ public EventDto CreateEvent(EventDto eventDto) var newEvent = eventDto.ToEntity(); newEvent.CreatedAt = createdTime; newEvent.UpdatedAt = createdTime; - newEvent.Slug = CreateSlug(eventDto, createdTime); + newEvent.Slug = CreateSlug(eventDto); context.Events.Add(newEvent); @@ -257,24 +257,25 @@ public IEnumerable GetUserEvents(string username) return userEvents.ToDtoList().AsEnumerable(); } - private string CreateSlug(EventDto eventDto, DateTime createdTime) + private string CreateSlug(EventDto eventDto) { - var slug = eventDto.Name.ToLower(); - slug = slug.Trim(); - slug = Regex.Replace(slug, @"\s+", " "); - slug = Regex.Replace(slug, " ", "-"); - slug = Regex.Replace(slug, @"[^a-z0-9-]", ""); - slug = Regex.Replace(slug, "-+", "-"); + var baseSlug = eventDto.Name.ToLower(); + baseSlug = baseSlug.Trim(); + baseSlug = Regex.Replace(baseSlug, @"\s+", " "); + baseSlug = Regex.Replace(baseSlug, " ", "-"); + baseSlug = Regex.Replace(baseSlug, @"[^a-z0-9-]", ""); + baseSlug = Regex.Replace(baseSlug, "-+", "-"); - var timestamp = createdTime.Ticks.ToString(); - var maxSlugLength = 128 - timestamp.Length - 1; // -1 for the dash between slug and timestamp + var slug = baseSlug; + var counter = 1; - if (slug.Length > maxSlugLength) + while (context.Events.Any(e => e.Slug == slug)) { - slug = slug.Substring(0, maxSlugLength); + slug = $"{baseSlug}-{counter}"; + counter++; } - return $"{slug}-{timestamp}"; + return slug; } } \ No newline at end of file From 08462e198cc5b2d879cdb1a647105b75e636cf60 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Fri, 14 Jun 2024 00:14:55 +0200 Subject: [PATCH 60/65] changes --- .../ReasnAPI/Services/EventService.cs | 30 +++++++++++++++---- .../ReasnAPI/Services/ImageService.cs | 2 -- .../ReasnAPI/Services/ParameterService.cs | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 2e90bd9f..e18e0de1 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -266,16 +266,36 @@ private string CreateSlug(EventDto eventDto) baseSlug = Regex.Replace(baseSlug, @"[^a-z0-9-]", ""); baseSlug = Regex.Replace(baseSlug, "-+", "-"); - var slug = baseSlug; + var existingSlugs = context.Events + .Where(e => e.Slug.StartsWith(baseSlug)) + .Select(e => e.Slug) + .ToList(); + var counter = 1; + if (existingSlugs.Any()) + { + var regex = new Regex($"^{Regex.Escape(baseSlug)}-(\\d+)$"); + foreach (var slug in existingSlugs) + { + var match = regex.Match(slug); + if (match.Success) + { + var currentCounter = int.Parse(match.Groups[1].Value); + counter = Math.Max(counter, currentCounter + 1); + } + } + } - while (context.Events.Any(e => e.Slug == slug)) + var countLength = counter.ToString().Length; + var maxBaseSlugLength = 128 - countLength - 1; + if (baseSlug.Length > maxBaseSlugLength) { - slug = $"{baseSlug}-{counter}"; - counter++; + baseSlug = baseSlug.Substring(0, maxBaseSlugLength); } - return slug; + var finalSlug = $"{baseSlug}-{counter}"; + + return finalSlug; } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs index cdb3672c..59d688d4 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ImageService.cs @@ -72,7 +72,6 @@ public void UpdateImagesForEvent(int objectId, List imageDtos) { var images = context.Images.Where(r => r.ObjectId == objectId && r.ObjectType == ObjectType.Event).ToList(); - // Remove images that are not in the new list foreach (var image in images) { if (!imageDtos.Any(dto => dto.ImageData == image.ImageData)) @@ -81,7 +80,6 @@ public void UpdateImagesForEvent(int objectId, List imageDtos) } } - // Add new images that are not in the database var newImages = new List(); foreach (var imageDto in imageDtos) { diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 9aaece6b..3d758e4d 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -35,7 +35,7 @@ public ParameterDto UpdateParameter(int parameterId, ParameterDto parameterDto) var parameterCheck = parameters.FirstOrDefault(r => r.Parameters.Any(p => p.Id == parameterId)); - if (parameterCheck is not null) // if parameter is associated with an event, it cannot be updated + if (parameterCheck is not null) { throw new BadRequestException("Parameter is associated with an event"); } From 42230698a72650522183a9d4aaaf9d1d7210a930 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Fri, 14 Jun 2024 14:59:24 +0200 Subject: [PATCH 61/65] Created AddEventComment method --- .../ReasnAPI.Tests/Services/EventServicesTest.cs | 10 +++++----- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index 9b9da548..a844b4e7 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -80,7 +80,7 @@ public void UpdateEvent_EventExists_EventUpdated() mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List()); mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); - var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object)); var result = eventService.UpdateEvent(1, eventDto); Assert.AreEqual("name1", result.Name); @@ -142,7 +142,7 @@ public void UpdateEvent_EventDoesNotExist_NullReturned() UpdatedAt =DateTime.Now }}); - var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object)); Assert.ThrowsException(() => eventService.UpdateEvent(1, eventDto)); } @@ -203,7 +203,7 @@ public void GetEventById_EventExists_EventReturned() UpdatedAt =DateTime.Now }}); - var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object)); var result = eventService.GetEventById(1); Assert.IsNotNull(result); @@ -248,7 +248,7 @@ public void GetEventById_EventDoesNotExist_NullReturned() }}); - var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object)); Assert.ThrowsException(() => eventService.GetEventById(1)); } @@ -318,7 +318,7 @@ public void DeleteEvent_EventExists_EventDeleted() mockContext.Setup(c => c.Comments).ReturnsDbSet(new List()); mockContext.Setup(c => c.Participants).ReturnsDbSet(new List()); - var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object)); + var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object)); eventService.DeleteEvent(1); diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index e18e0de1..a8266a91 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -8,7 +8,7 @@ using ReasnAPI.Mappers; namespace ReasnAPI.Services; -public class EventService(ReasnContext context, ParameterService parameterService, TagService tagService) +public class EventService(ReasnContext context, ParameterService parameterService, TagService tagService, CommentService commentService) { public EventDto CreateEvent(EventDto eventDto) @@ -230,6 +230,14 @@ public IEnumerable GetEventCommentsBySlug(string slug) return commentDtos; } + public void AddEventComment(CommentDto commentDto, string slug) + { + commentDto = commentService.CreateComment(commentDto); + var relatedEvent = GetEventBySlug(slug); + relatedEvent.Comments.Add(commentDto.ToEntity()); + context.SaveChanges(); + } + public IEnumerable GetEventsByFilter(Expression> filter) { var events = context.Events.Include(e => e.Parameters).Include(e => e.Tags).Where(filter).ToList(); From 54001a67c378cb17cff0ff66745c78c15c9d813f Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Fri, 14 Jun 2024 15:44:25 +0200 Subject: [PATCH 62/65] Update EventService.cs --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index a8266a91..535de2b3 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -6,6 +6,7 @@ using System.Transactions; using System.Text.RegularExpressions; using ReasnAPI.Mappers; +using ReasnAPI.Models.Enums; namespace ReasnAPI.Services; public class EventService(ReasnContext context, ParameterService parameterService, TagService tagService, CommentService commentService) @@ -203,7 +204,7 @@ public Event GetEventBySlug(string slug) return eventToReturn; } - public IEnumerable GetEventParticipantsBySlug(string slug) + public int GetEventParticipantsBySlug(string slug, ParticipantStatus status) { var eventToReturn = context.Events.Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) @@ -211,9 +212,7 @@ public IEnumerable GetEventParticipantsBySlug(string slug) throw new NotFoundException("Event not found"); } - var participantsDto = eventToReturn.Participants.Select(p => p.ToDto()); - - return participantsDto; + return eventToReturn.Participants.Count(p => p.Status == status); } public IEnumerable GetEventCommentsBySlug(string slug) From 3a3274fc9dbf5319fdcb912295d6add047b800c7 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Sat, 15 Jun 2024 17:37:03 +0200 Subject: [PATCH 63/65] fixed updating and creating event --- .../ReasnAPI/Services/EventService.cs | 97 ++++++++----------- .../ReasnAPI/Services/ParameterService.cs | 20 ++-- .../ReasnAPI/ReasnAPI/Services/TagService.cs | 24 ++--- 3 files changed, 63 insertions(+), 78 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 535de2b3..22f90bcb 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -21,6 +21,7 @@ public EventDto CreateEvent(EventDto eventDto) newEvent.CreatedAt = createdTime; newEvent.UpdatedAt = createdTime; newEvent.Slug = CreateSlug(eventDto); + context.Events.Add(newEvent); @@ -28,17 +29,18 @@ public EventDto CreateEvent(EventDto eventDto) if (eventDto.Tags != null) { var newTags = eventDto.Tags.ToEntityList(); - AttatchTagsToEvent(newTags, newEvent); + tagService.AttatchTagsToEvent(newTags, newEvent); } if (eventDto.Parameters != null) { var newParameters = eventDto.Parameters.ToEntityList(); - AttachParametersToEvent(newParameters, newEvent); + parameterService.AttachParametersToEvent(newParameters, newEvent); } context.SaveChanges(); scope.Complete(); + eventDto.Slug = newEvent.Slug; } return eventDto; @@ -75,7 +77,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) .Where(existingTag => newTags.All(newTag => newTag.Name != existingTag.Name)) .ToList(); DetachTagsFromEvent(tagsToRemove, eventToUpdate); - AttatchTagsToEvent(newTags, eventToUpdate); + tagService.AttatchTagsToEvent(newTags, eventToUpdate); } if (eventDto.Parameters == null) @@ -89,7 +91,7 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) .Where(existingParam => newParameters.All(newParam => newParam.Key != existingParam.Key || newParam.Value != existingParam.Value)) .ToList(); DetachParametersFromEvent(paramsToRemove, eventToUpdate); - AttachParametersToEvent(newParameters, eventToUpdate); + parameterService.AttachParametersToEvent(newParameters, eventToUpdate); } context.Events.Update(eventToUpdate); @@ -101,19 +103,6 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) return eventDto; } - private void AttatchTagsToEvent(List tagsToAdd, Event eventToUpdate) - { - tagService.AddTagsFromList(tagsToAdd); - - var tagsToAttach = tagsToAdd - .Where(newTag => eventToUpdate.Tags.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); - - tagsToAttach.ForEach(eventToUpdate.Tags.Add); - - context.SaveChanges(); - } - private void DetachTagsFromEvent(List tagsToRemove, Event eventToUpdate) { tagsToRemove.ForEach(tag => eventToUpdate.Tags.Remove(tag)); @@ -122,19 +111,6 @@ private void DetachTagsFromEvent(List tagsToRemove, Event eventToUpdate) tagService.RemoveTagsNotInAnyEvent(); } - private void AttachParametersToEvent(List parametersToAdd, Event eventToUpdate) - { - parameterService.AddParametersFromList(parametersToAdd); - - var paramsToAttach = parametersToAdd - .Where(newParam => eventToUpdate.Parameters.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) - .ToList(); - - paramsToAttach.ForEach(eventToUpdate.Parameters.Add); - - context.SaveChanges(); - } - private void DetachParametersFromEvent(List parametersToRemove, Event eventToUpdate) { parametersToRemove.ForEach(param => eventToUpdate.Parameters.Remove(param)); @@ -204,7 +180,7 @@ public Event GetEventBySlug(string slug) return eventToReturn; } - public int GetEventParticipantsBySlug(string slug, ParticipantStatus status) + public int GetEventParticipantsCountBySlugAndStatus(string slug, ParticipantStatus status) { var eventToReturn = context.Events.Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); if (eventToReturn is null) @@ -215,6 +191,21 @@ public int GetEventParticipantsBySlug(string slug, ParticipantStatus status) return eventToReturn.Participants.Count(p => p.Status == status); } + public IEnumerable GetEventParticipantsBySlugAndStatus(string slug, ParticipantStatus status) + { + var eventToReturn = context.Events.Include(e => e.Participants).FirstOrDefault(e => e.Slug == slug); + if (eventToReturn is null) + { + throw new NotFoundException("Event not found"); + } + + var participantDtos = eventToReturn.Participants + .Where(p => p.Status == status) + .Select(p => p.ToDto()); + + return participantDtos; + } + public IEnumerable GetEventCommentsBySlug(string slug) { var eventToReturn = context.Events.Include(e => e.Comments) @@ -266,43 +257,33 @@ public IEnumerable GetUserEvents(string username) private string CreateSlug(EventDto eventDto) { - var baseSlug = eventDto.Name.ToLower(); - baseSlug = baseSlug.Trim(); - baseSlug = Regex.Replace(baseSlug, @"\s+", " "); - baseSlug = Regex.Replace(baseSlug, " ", "-"); - baseSlug = Regex.Replace(baseSlug, @"[^a-z0-9-]", ""); - baseSlug = Regex.Replace(baseSlug, "-+", "-"); + var baseSlug = Regex.Replace(eventDto.Name.ToLower().Trim(), @"[^a-z0-9\s-]", "") + .Replace(" ", "-") + .Replace("--", "-"); var existingSlugs = context.Events - .Where(e => e.Slug.StartsWith(baseSlug)) + .Where(e => EF.Functions.Like(e.Slug, $"{baseSlug}%")) .Select(e => e.Slug) + .AsNoTracking() .ToList(); - var counter = 1; - if (existingSlugs.Any()) - { - var regex = new Regex($"^{Regex.Escape(baseSlug)}-(\\d+)$"); - foreach (var slug in existingSlugs) - { - var match = regex.Match(slug); - if (match.Success) - { - var currentCounter = int.Parse(match.Groups[1].Value); - counter = Math.Max(counter, currentCounter + 1); - } - } - } + var highestNumber = existingSlugs + .Select(slug => Regex.Match(slug, $"^{Regex.Escape(baseSlug)}-(\\d+)$")) + .Where(match => match.Success) + .Select(match => int.Parse(match.Groups[1].Value)) + .DefaultIfEmpty(0) + .Max(); + + var counter = highestNumber + 1; + var finalSlug = $"{baseSlug}-{counter}"; - var countLength = counter.ToString().Length; - var maxBaseSlugLength = 128 - countLength - 1; - if (baseSlug.Length > maxBaseSlugLength) + if (finalSlug.Length > 128) { - baseSlug = baseSlug.Substring(0, maxBaseSlugLength); + finalSlug = $"{baseSlug[..(128 - counter.ToString().Length - 1)]}-{counter}"; } - var finalSlug = $"{baseSlug}-{counter}"; - return finalSlug; + } } \ No newline at end of file diff --git a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs index 3d758e4d..f38c8534 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/ParameterService.cs @@ -104,17 +104,25 @@ public void RemoveParametersNotInAnyEvent() context.SaveChanges(); } - public void AddParametersFromList(List parametersToAdd) + public void AttachParametersToEvent(List parametersToAdd, Event eventToUpdate) { - var existingParamsInDb = context.Parameters - .Where(param => parametersToAdd.Any(newParam => newParam.Key == param.Key && newParam.Value == param.Value)) + var parameterKeyValuePairsToAdd = parametersToAdd.Select(p => new { p.Key, p.Value }).ToList(); + + var keysToAdd = parametersToAdd.Select(p => p.Key).Distinct().ToList(); + var valuesToAdd = parametersToAdd.Select(p => p.Value).Distinct().ToList(); + + var parametersFromDb = context.Parameters + .Where(param => keysToAdd.Contains(param.Key) && valuesToAdd.Contains(param.Value)) .ToList(); - var paramsToAdd = parametersToAdd - .Where(newParam => existingParamsInDb.All(existingParam => existingParam.Key != newParam.Key || existingParam.Value != newParam.Value)) + parametersFromDb.ForEach(eventToUpdate.Parameters.Add); + + var newParametersToAdd = parametersToAdd.Where(paramToAdd => + !parametersFromDb.Any(existingParam => existingParam.Key == paramToAdd.Key && existingParam.Value == paramToAdd.Value)) .ToList(); - context.Parameters.AddRange(paramsToAdd); + newParametersToAdd.ForEach(eventToUpdate.Parameters.Add); + context.SaveChanges(); } diff --git a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs index 6f9f9f55..65f27409 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/TagService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/TagService.cs @@ -38,22 +38,18 @@ public TagDto UpdateTag(int tagId, TagDto tagDto) return tagDto; } - public void AddTagsFromList(List tagsToAdd) - { - if (tagsToAdd is null) - { - throw new ArgumentException("No tag provided"); - } - - var existingTagsInDb = context.Tags - .Where(tag => tagsToAdd.Any(newTag => newTag.Name == tag.Name)) - .ToList(); + public void AttatchTagsToEvent(List tagsToAdd, Event eventToUpdate) + { - var tagsToAddToDb = tagsToAdd - .Where(newTag => existingTagsInDb.All(existingTag => existingTag.Name != newTag.Name)) - .ToList(); + var tagNamesToAdd = tagsToAdd.Select(t => t.Name).ToList(); + + var tagsFromDb = context.Tags.Where(tag => tagNamesToAdd.Contains(tag.Name)).ToList(); + + tagsFromDb.ForEach(eventToUpdate.Tags.Add); + + var newTagsToAdd = tagsToAdd.Where(t => tagsFromDb.All(dbTag => dbTag.Name != t.Name)).ToList(); - context.Tags.AddRange(tagsToAddToDb); + newTagsToAdd.ForEach(eventToUpdate.Tags.Add); context.SaveChanges(); } From fc3c82fb84ef144e9798cb6c6b229a591927c2ad Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 17 Jun 2024 13:25:19 +0200 Subject: [PATCH 64/65] Update EventService.cs --- Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 22f90bcb..1eddad96 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -56,6 +56,10 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto) { throw new NotFoundException("Event not found"); } + if (eventToUpdate.Name != eventDto.Name) + { + eventToUpdate.Slug = CreateSlug(eventDto); + } eventToUpdate.Name = eventDto.Name; eventToUpdate.AddressId = eventDto.AddressId; From f5dace19b52530ce0a6340bd680f3a6eb1bd4688 Mon Sep 17 00:00:00 2001 From: Owczarek Kamil Date: Mon, 17 Jun 2024 13:35:40 +0200 Subject: [PATCH 65/65] changed create slug and text for event --- .../ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs | 2 ++ Server/ReasnAPI/ReasnAPI/Services/EventService.cs | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs index a844b4e7..fc504b17 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs +++ b/Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs @@ -31,6 +31,7 @@ public void UpdateEvent_EventExists_EventUpdated() EndAt = DateTime.Now, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, + Slug = "name1", Status = EventStatus.Completed, Tags = tagList, }; @@ -47,6 +48,7 @@ public void UpdateEvent_EventExists_EventUpdated() StartAt = DateTime.Now, EndAt = DateTime.Now, CreatedAt = DateTime.Now, + Slug = "name1", UpdatedAt = DateTime.Now, Status = EventStatus.Completed, }}); diff --git a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs index 1eddad96..3e3d3709 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/EventService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/EventService.cs @@ -265,11 +265,13 @@ private string CreateSlug(EventDto eventDto) .Replace(" ", "-") .Replace("--", "-"); + var pattern = $"^{Regex.Escape(baseSlug)}(-\\d+)?$"; + var existingSlugs = context.Events - .Where(e => EF.Functions.Like(e.Slug, $"{baseSlug}%")) .Select(e => e.Slug) .AsNoTracking() - .ToList(); + .AsEnumerable() + .Where(slug => Regex.IsMatch(slug, pattern)); var highestNumber = existingSlugs .Select(slug => Regex.Match(slug, $"^{Regex.Escape(baseSlug)}-(\\d+)$"))