using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using MoonCore.Extended.Abstractions; using MoonCore.Helpers; using Moonlight.ApiServer.App.Attributes; using Moonlight.ApiServer.App.Exceptions; using Moonlight.ApiServer.App.Helpers; using MoonlightServers.ApiServer.Database.Entities; using MoonlightServers.Shared.Http.Requests.Admin.Allocations; using MoonlightServers.Shared.Http.Responses.Admin.Allocations; namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes; [ApiController] [Route("admin/servers/nodes/{rootItem:int}/allocations")] public class NodeAllocationsController : BaseSubCrudController { public override Func> Property => node => node.Allocations; public NodeAllocationsController(DatabaseRepository itemRepository, DatabaseRepository rootItemRepository, IHttpContextAccessor contextAccessor) : base(itemRepository, rootItemRepository, contextAccessor) { PermissionPrefix = "admin.servers.nodes.allocations"; } [HttpPost] [RequirePermission("admin.servers.nodes.allocations.create")] public override async Task> Create(CreateAllocationRequest request) { if (ItemRepository.Get().Any(x => x.IpAddress == request.IpAddress && x.Port == request.Port)) throw new ApiException("An allocation with this ip and port already exists", statusCode: 400); var item = Mapper.Map(request!); Property.Invoke(RootItem).Add(item); RootItemRepository.Update(RootItem); var response = Mapper.Map(item); return Ok(response); } [HttpPatch("{id}")] [RequirePermission("admin.servers.nodes.allocations.create")] public override async Task> Update(int id, UpdateAllocationRequest request) { var item = LoadItemById(id); if (ItemRepository.Get().Any(x => x.IpAddress == request.IpAddress && x.Port == request.Port && x.Id != item.Id)) throw new ApiException("An allocation with this ip and port already exists", statusCode: 400); var mappedItem = Mapper.Map(item, request!, ignoreNullValues: true); ItemRepository.Update(mappedItem); var response = Mapper.Map(mappedItem); return Ok(response); } protected override IEnumerable IncludeRelations(IQueryable items) => items.Include(x => x.Allocations); }