Skip to content
Open
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
1 change: 1 addition & 0 deletions Api/Controllers/ItemTypesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Api.Requests;
using Api.Responses;
using Application.Commands;
using Application.Features.Dtos;
using Application.Queries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.ComponentModel.DataAnnotations;
using Api.Features.Items.CreateItem;
using Api.Features.Items.GetAllItems;
using Api.ExternalDeps.EmployeesApi;
using Application.Commands;
using Application.Features.Items.CreateItem;
using Application.Features.Items.GetAllItems;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TourmalineCore.AspNetCore.JwtAuthentication.Core.Filters;

namespace Api.Features.Items;
namespace Api.Controllers;

[Authorize]
[ApiController]
Expand All @@ -18,11 +19,14 @@ public class ItemsController : ControllerBase
/// </summary>
[RequiresPermission(UserClaimsProvider.CanViewItems)]
[HttpGet]
public Task<GetAllItemsResponse> GetAllItemsAsync(
[FromServices] GetAllItemsHandler getAllItemsHandler
public async Task<GetAllItemsResponse> GetAllItemsAsync(
[FromServices] GetAllItemsHandler getAllItemsHandler,
[FromServices] EmployeesApi employeesApi
)
{
return getAllItemsHandler.HandleAsync();
var allEmployeesResponse = await employeesApi.GetAllEmployeesAsync();

return await getAllItemsHandler.HandleAsync(allEmployeesResponse);
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions Api/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Api.ExternalDeps.EmployeesApi;
using Api.Features.Items.CreateItem;
using Api.Features.Items.GetAllItems;
using Application;
using Application.Commands;
using Application.Features.Items.CreateItem;
using Application.Features.Items.GetAllItems;
using Application.Queries;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -35,7 +35,6 @@ public static void AddApplication(this IServiceCollection services, IConfigurati
services.AddTransient<HardDeleteItemTypeCommand>();
services.AddTransient<AllItemTypesQuery>();

services.AddTransient<CreateItemCommand>();
services.AddTransient<AllItemsQuery>();
services.AddTransient<HardDeleteItemCommand>();

Expand Down
2 changes: 1 addition & 1 deletion Api/ExternalDeps/EmployeesApi/EmployeesApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Api.ExternalDeps.EmployeesApi.Responses;
using Application.ExternalDeps.EmployeesApi;
using Microsoft.Extensions.Options;
using TourmalineCore.AspNetCore.JwtAuthentication.Core.Options;

Expand Down
34 changes: 0 additions & 34 deletions Api/Features/Items/CreateItem/CreateItemHandler.cs

This file was deleted.

22 changes: 0 additions & 22 deletions Api/Features/Items/GetAllItems/HolderEmployeeMapper.cs

This file was deleted.

23 changes: 0 additions & 23 deletions Api/Features/Items/GetAllItems/HolderEmployeeMapperTests.cs

This file was deleted.

4 changes: 3 additions & 1 deletion Api/Responses/ItemTypesResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Api.Responses;
using Application.Features.Dtos;

namespace Api.Responses;

public class ItemTypesResponse
{
Expand Down
69 changes: 0 additions & 69 deletions Application/Commands/CreateItemCommand.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Api.ExternalDeps.EmployeesApi.Responses;
namespace Application.ExternalDeps.EmployeesApi;

public class EmployeesResponse
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Api.Responses;
namespace Application.Features.Dtos;

public class ItemTypeDto
{
Expand Down
54 changes: 54 additions & 0 deletions Application/Features/Items/CreateItem/CreateItemHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Core.Entities;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Items.CreateItem;

public class CreateItemHandler
{
private readonly TenantAppDbContext _context;
private readonly IClaimsProvider _claimsProvider;

public CreateItemHandler(
TenantAppDbContext context,
IClaimsProvider claimsProvider
)
{
_context = context;
_claimsProvider = claimsProvider;
}

public async Task<CreateItemResponse> HandleAsync(CreateItemRequest createItemRequest)
{
var itemTypeIdDoesNotExistWithinTenant = await _context
.QueryableWithinTenant<ItemType>()
.AllAsync(x => x.Id != createItemRequest.ItemTypeId);

if (itemTypeIdDoesNotExistWithinTenant)
{
throw new Exception($"Passed item type where id={createItemRequest.ItemTypeId} is not found within tenant where id={_claimsProvider.TenantId}");
}

var item = new Item
{
TenantId = _claimsProvider.TenantId,
Name = createItemRequest.Name,
SerialNumber = createItemRequest.SerialNumber,
ItemTypeId = createItemRequest.ItemTypeId,
Price = createItemRequest.Price,
Description = createItemRequest.Description,
PurchaseDate = createItemRequest.PurchaseDate,
HolderEmployeeId = createItemRequest.HolderEmployeeId
};

await _context
.Items
.AddAsync(item);

await _context.SaveChangesAsync();

return new CreateItemResponse()
{
NewItemId = item.Id
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace Api.Features.Items.CreateItem;
namespace Application.Features.Items.CreateItem;

public class CreateItemRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Api.Features.Items.CreateItem;
namespace Application.Features.Items.CreateItem;

public class CreateItemResponse
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
using Api.ExternalDeps.EmployeesApi;
using Api.Responses;
using Application.ExternalDeps.EmployeesApi;
using Application.Features.Dtos;
using Application.Queries;

namespace Api.Features.Items.GetAllItems;
namespace Application.Features.Items.GetAllItems;

public class GetAllItemsHandler
{
private readonly AllItemsQuery _allItemsQuery;
private readonly EmployeesApi _employeesApi;

public const string NotFoundEmployeeFullName = "Not Found";

public GetAllItemsHandler(
AllItemsQuery allItemsQuery,
EmployeesApi employeesApi
AllItemsQuery allItemsQuery
)
{
_allItemsQuery = allItemsQuery;
_employeesApi = employeesApi;
}

public async Task<GetAllItemsResponse> HandleAsync()
public async Task<GetAllItemsResponse> HandleAsync(EmployeesResponse allEmployeesResponse)
{
var items = await _allItemsQuery.GetAsync();

var allEmployeesResponse = await _employeesApi.GetAllEmployeesAsync();

return new GetAllItemsResponse
{
Items = items
Expand All @@ -40,7 +37,16 @@ public async Task<GetAllItemsResponse> HandleAsync()
Price = x.Price,
Description = x.Description,
PurchaseDate = x.PurchaseDate,
HolderEmployee = HolderEmployeeMapper.MapToEmployeeDto(x.HolderEmployeeId, allEmployeesResponse)
HolderEmployee = x.HolderEmployeeId == null
? null
: new EmployeeDto
{
Id = x.HolderEmployeeId.Value,
FullName = allEmployeesResponse
.Employees
.SingleOrDefault(y => y.Id == x.HolderEmployeeId.Value)
?.FullName ?? NotFoundEmployeeFullName
}
})
.ToList()
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Api.Responses;
using Application.Features.Dtos;

namespace Api.Features.Items.GetAllItems;
namespace Application.Features.Items.GetAllItems;

public class GetAllItemsResponse
{
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# inner-circle-items-api

<!-- auto-generated -->
[![coverage](https://img.shields.io/badge/e2e_coverage-82.22%25-olivedrab)](https://github.com/TourmalineCore/inner-circle-items-api/actions/workflows/calculate-tests-coverage-on-pull-request.yml)
[![coverage](https://img.shields.io/badge/units_coverage-17.34%25-crimson)](https://github.com/TourmalineCore/inner-circle-items-api/actions/workflows/calculate-tests-coverage-on-pull-request.yml)
[![coverage](https://img.shields.io/badge/full_coverage-89.96%25-olivedrab)](https://github.com/TourmalineCore/inner-circle-items-api/actions/workflows/calculate-tests-coverage-on-pull-request.yml)
[![coverage](https://img.shields.io/badge/e2e_coverage-82.47%25-olivedrab)](https://github.com/TourmalineCore/inner-circle-items-api/actions/workflows/calculate-tests-coverage-on-pull-request.yml)
[![coverage](https://img.shields.io/badge/units_coverage-15.80%25-crimson)](https://github.com/TourmalineCore/inner-circle-items-api/actions/workflows/calculate-tests-coverage-on-pull-request.yml)
[![coverage](https://img.shields.io/badge/full_coverage-89.59%25-olivedrab)](https://github.com/TourmalineCore/inner-circle-items-api/actions/workflows/calculate-tests-coverage-on-pull-request.yml)
<!-- auto-generated -->

This repo contains Inner Circle Items API.
Expand Down