Skip to content

Commit

Permalink
created mappers and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 9, 2024
1 parent 6ad0545 commit 4c50d36
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 175 deletions.
6 changes: 4 additions & 2 deletions Server/ReasnAPI/ReasnAPI/Models/DTOs/InterestDto.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
}
6 changes: 2 additions & 4 deletions Server/ReasnAPI/ReasnAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
dataSourceBuilder.MapEnum<UserRole>("users.role");
var dataSource = dataSourceBuilder.Build();

builder.Services.AddDbContext<ReasnContext>(options =>
options.UseNpgsql(dataSource).LogTo(s => System.Diagnostics.Debug.WriteLine(s))
.EnableDetailedErrors(true)
.EnableSensitiveDataLogging(true));
builder.Services.AddDbContext<ReasnContext>(options =>
options.UseNpgsql(dataSource));

builder.Services.AddScoped<InterestService>();
builder.Services.AddScoped<TagService>();
Expand Down
3 changes: 3 additions & 0 deletions Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Helpers\" />
<Folder Include="Models\" />
<Folder Include="Services\" />
<Folder Include="Validators\" />
</ItemGroup>

Expand Down
165 changes: 59 additions & 106 deletions Server/ReasnAPI/ReasnAPI/Services/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,28 @@ 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))
.Select(t => new Tag { Name = t.Name })
.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))
Expand All @@ -58,7 +45,7 @@ public class EventService(ReasnContext context)

context.Parameters.AddRange(newParameters);

addedEvent.Parameters = newParameters;
newEvent.Parameters = newParameters;
}

context.SaveChanges();
Expand Down Expand Up @@ -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)
{

Expand All @@ -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();
Expand All @@ -130,7 +108,7 @@ public class EventService(ReasnContext context)
newTag)
.ToList();

existingEvent.Tags = updatedTags;
eventToUpdate.Tags = updatedTags;

context.SaveChanges();
}
Expand All @@ -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();
Expand All @@ -166,7 +144,7 @@ public class EventService(ReasnContext context)
newParam)
.ToList();

existingEvent.Parameters = updatedParams;
eventToUpdate.Parameters = updatedParams;

context.SaveChanges();
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -236,29 +192,7 @@ public IEnumerable<EventDto> GetEventsByFilter(Expression<Func<Event, bool>> fil
var eventDtos = new List<EventDto>();
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);
}
Expand All @@ -273,29 +207,7 @@ public IEnumerable<EventDto> GetAllEvents()
var eventDtos = new List<EventDto>();
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);
}
Expand All @@ -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
};
}

}
55 changes: 25 additions & 30 deletions Server/ReasnAPI/ReasnAPI/Services/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ public List<ImageDto> CreateImages(List<ImageDto> imageDtos)
continue;
}

var newImage = new Image
{
ImageData = imageDto.ImageData,
ObjectId = imageDto.ObjectId,
ObjectType = imageDto.ObjectType
};

var newImage = MapImageFromImageDto(imageDto);

newImages.Add(newImage);
}

Expand Down Expand Up @@ -89,26 +84,16 @@ 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;
}

public IEnumerable<ImageDto> 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;
}
Expand All @@ -117,12 +102,7 @@ public IEnumerable<ImageDto> GetImagesByFilter(Expression<Func<Image, bool>> 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;
}
Expand All @@ -133,14 +113,29 @@ public IEnumerable<ImageDto> 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
};
}

}
Loading

0 comments on commit 4c50d36

Please sign in to comment.