Skip to content

Commit

Permalink
Updated Parameter, Tag, Status services
Browse files Browse the repository at this point in the history
Checked if any of Parameters, Tags or Statuses are used before delating/updating.
  • Loading branch information
bilimig committed May 1, 2024
1 parent 8c2820d commit 8ecbebe
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 91 deletions.
479 changes: 478 additions & 1 deletion Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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());
}
Expand Down
34 changes: 34 additions & 0 deletions Server/ReasnAPI/ReasnAPI/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
105 changes: 64 additions & 41 deletions Server/ReasnAPI/ReasnAPI/Services/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -27,7 +27,7 @@ public EventDto CreateEvent(EventDto eventDto)
Slug = eventDto.Slug,
StatusId = eventDto.StatusId,
};

context.Events.Add(newEvent);
context.SaveChanges();

Expand All @@ -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();
}
}
}

Expand Down Expand Up @@ -84,25 +98,25 @@ public EventDto UpdateEvent(int eventId, EventDto eventDto)
{
return eventDto;
}

var newTags = eventDto.Tags
.Select(tagDto => new Tag
{
Name = tagDto.Name,
}).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)
{
context.EventTags.Remove(existingTag);
}

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)
{
Expand All @@ -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();
}

Expand All @@ -133,28 +147,31 @@ public void DeleteEvent(int eventId)
{
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)
if (eventToReturn == null)
{
return null;
}

var eventTags = context.EventTags.Where(r => r.EventId == eventId).Include(e => e.Tag).ToList();
var tags = new List<TagDto>();
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
{
Expand All @@ -180,12 +197,15 @@ public IEnumerable<EventDto> GetEventsByFilter(Expression<Func<Event, bool>> fil
var eventDtos = new List<EventDto>();
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<TagDto>();
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
{
Expand Down Expand Up @@ -214,12 +234,15 @@ public IEnumerable<EventDto> GetAllEvents()
var eventDtos = new List<EventDto>();
foreach (var eventToReturn in events)
{
var eventTags = context.EventTags.Where(r => r.EventId == eventToReturn.Id).Include(e=> e.Tag).ToList();
var tags = new List<TagDto>();
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
{
Expand Down Expand Up @@ -247,5 +270,5 @@ private string CreateSlug(EventDto eventDto)
var slug = eventDto.Name.ToLower().Replace(" ", "-");
return slug;
}
}

}
83 changes: 63 additions & 20 deletions Server/ReasnAPI/ReasnAPI/Services/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageDto> CreateImages(List<ImageDto> imageDtos)
{
var image = context.Images.FirstOrDefault(r => r.ObjectId == imageDto.ObjectId && r.ObjectTypeId == imageDto.ObjectTypeId);
if (image != null)
var newImages = new List<Image>();

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)
Expand All @@ -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)
Expand All @@ -74,7 +100,7 @@ public ImageDto GetImageById(int id)
public IEnumerable<ImageDto> GetAllImages()
{
var images = context.Images.ToList();

var imageDtos = images.Select(image => new ImageDto
{
ImageData = image.ImageData,
Expand All @@ -97,5 +123,22 @@ public IEnumerable<ImageDto> GetImagesByFilter(Expression<Func<Image, bool>> fil
}).ToList();

return imageDtos;
}
}
}

public IEnumerable<ImageDto> 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;
}

}
Loading

0 comments on commit 8ecbebe

Please sign in to comment.