Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TickAPI/TickAPI.Tests/Events/Services/EventServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public async Task CreateNewEventAsync_WhenEventDataIsValid_ShouldReturnNewEvent(
dateTimeServiceMock.Setup(m => m.GetCurrentDateTime()).Returns(new DateTime(2003, 7, 11));

var categoryServiceMock = new Mock<ICategoryService>();
categoryServiceMock.Setup(c => c.CheckIfCategoriesExistAsync(It.IsAny<List<Category>>())).Returns(Task.FromResult(true));
categoryServiceMock.Setup(c => c.GetCategoriesByNames(It.IsAny<List<string>>())).Returns(Result<List<Category>>.Success(expectedCategories));

var paginationServiceMock = new Mock<IPaginationService>();

Expand Down
1 change: 0 additions & 1 deletion TickAPI/TickAPI/Addresses/Models/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ public class Address
public uint? HouseNumber { get; set; }
public uint? FlatNumber { get; set; }
public string PostalCode { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface ICategoryRepository
{
public IQueryable<Category> GetCategories();
public Task<Result<Category>> GetCategoryByNameAsync(string categoryName);
public Result<IQueryable<Category>> GetCategoriesByNames(IEnumerable<string> categoryNames);
public Task AddNewCategoryAsync(Category category);
}
3 changes: 1 addition & 2 deletions TickAPI/TickAPI/Categories/Abstractions/ICategoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ public interface ICategoryService
public Task<Result<Category>> GetCategoryByNameAsync(string categoryName);
public Task<Result<PaginatedData<GetCategoryResponseDto>>> GetCategoriesResponsesAsync(int pageSize, int page);
public Task<Result<Category>> CreateNewCategoryAsync(string categoryName);

public Task<bool> CheckIfCategoriesExistAsync(IEnumerable<Category> categories);
public Result<List<Category>> GetCategoriesByNames(IList<string> categoryNames);
}
13 changes: 13 additions & 0 deletions TickAPI/TickAPI/Categories/Respositories/CategoryRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using TickAPI.Categories.Abstractions;
using TickAPI.Categories.Models;
using TickAPI.Common.Results;
using TickAPI.Common.Results.Generic;
using TickAPI.Common.TickApiDbContext;
namespace TickAPI.Categories.Respositories;
Expand Down Expand Up @@ -31,6 +32,18 @@ public async Task<Result<Category>> GetCategoryByNameAsync(string categoryName)
return Result<Category>.Success(category);
}

public Result<IQueryable<Category>> GetCategoriesByNames(IEnumerable<string> categoryNames)
{
var categories = _tickApiDbContext.Categories.Where(c => categoryNames.Contains(c.Name));

if (categories.Count() < categoryNames.Count())
{
return Result<IQueryable<Category>>.Failure(StatusCodes.Status404NotFound, "one or more of category names is invalid");
}

return Result<IQueryable<Category>>.Success(categories);
}

public async Task AddNewCategoryAsync(Category category)
{
_tickApiDbContext.Categories.Add(category);
Expand Down
16 changes: 11 additions & 5 deletions TickAPI/TickAPI/Categories/Services/CategoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using TickAPI.Common.Pagination.Abstractions;
using TickAPI.Common.Pagination.Responses;
using TickAPI.Common.Results.Generic;
using TickAPI.Events.Models;

namespace TickAPI.Categories.Services;

Expand Down Expand Up @@ -56,11 +57,16 @@ public async Task<Result<Category>> CreateNewCategoryAsync(string categoryName)
await _categoryRepository.AddNewCategoryAsync(category);
return Result<Category>.Success(category);
}
public async Task<bool> CheckIfCategoriesExistAsync(IEnumerable<Category> categories)

public Result<List<Category>> GetCategoriesByNames(IList<string> categoryNames)
{
var dbCategories = _categoryRepository.GetCategories();
int count = await dbCategories.Where(cdb => categories.Any(c => c.Name == cdb.Name)).CountAsync();
return count == categories.Count();
var result = _categoryRepository.GetCategoriesByNames(categoryNames);

if (result.IsError)
{
return Result<List<Category>>.PropagateError(result);
}

return Result<List<Category>>.Success(result.Value!.ToList());
}
}
2 changes: 1 addition & 1 deletion TickAPI/TickAPI/Events/Repositories/EventRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace TickAPI.Events.Repositories;

public class EventRepository : IEventRepository
{

private readonly TickApiDbContext _tickApiDbContext;

public EventRepository(TickApiDbContext tickApiDbContext)
{
_tickApiDbContext = tickApiDbContext;
}

public async Task AddNewEventAsync(Event @event)
{
_tickApiDbContext.Events.Add(@event);
Expand Down
12 changes: 6 additions & 6 deletions TickAPI/TickAPI/Events/Services/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using TickAPI.Common.Pagination.Responses;
using TickAPI.Categories.Abstractions;
using TickAPI.Categories.DTOs.Request;
using TickAPI.Categories.Models;
using TickAPI.Common.Time.Abstractions;
using TickAPI.Events.Abstractions;
using TickAPI.Events.Models;
Expand Down Expand Up @@ -61,12 +60,13 @@ public async Task<Result<Event>> CreateNewEventAsync(string name, string descri

var address = await _addressService.GetOrCreateAddressAsync(createAddress);

var categoriesConverted = categories.Select(c => new Category { Name = c.CategoryName }).ToList();
var categoryNames = categories.Select(c => c.CategoryName).ToList();

var categoriesExist = await _categoryService.CheckIfCategoriesExistAsync(categoriesConverted);
if (!categoriesExist)
var categoriesByNameResult = _categoryService.GetCategoriesByNames(categoryNames);

if (categoriesByNameResult.IsError)
{
return Result<Event>.Failure(StatusCodes.Status400BadRequest, "Category does not exist");
return Result<Event>.PropagateError(categoriesByNameResult);
}

var ticketTypesConverted = ticketTypes.Select(t => new TicketType
Expand All @@ -87,7 +87,7 @@ public async Task<Result<Event>> CreateNewEventAsync(string name, string descri
EndDate = endDate,
MinimumAge = minimumAge,
Address = address.Value!,
Categories = categoriesConverted,
Categories = categoriesByNameResult.Value!,
Organizer = organizerResult.Value!,
EventStatus = eventStatus,
TicketTypes = ticketTypesConverted,
Expand Down
Loading