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/Program.cs b/Server/ReasnAPI/ReasnAPI/Program.cs index aa9d3592..39a64dbf 100644 --- a/Server/ReasnAPI/ReasnAPI/Program.cs +++ b/Server/ReasnAPI/ReasnAPI/Program.cs @@ -22,10 +22,8 @@ dataSourceBuilder.MapEnum("users.role"); var dataSource = dataSourceBuilder.Build(); -builder.Services.AddDbContext(options => - options.UseNpgsql(dataSource).LogTo(s => System.Diagnostics.Debug.WriteLine(s)) - .EnableDetailedErrors(true) - .EnableSensitiveDataLogging(true)); +builder.Services.AddDbContext(options => + options.UseNpgsql(dataSource)); builder.Services.AddScoped(); builder.Services.AddScoped(); 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