public async Task AddResourceAsync(AddResourceToRoomCommand command) { Check.NotNull(command, nameof(command)); var resource = await _applicationContext.Resources .Where(x => x.Id == command.ResourceId) .FirstOrDefaultAsync(); Check.NotNull(resource, nameof(resource)); if (resource.Specific) { var checkSpecificResourceAlreadyAdded = await _applicationContext.RoomResources .Where(x => x.Resource.Specific && x.RoomId == command.RoomId) .AnyAsync(); if (checkSpecificResourceAlreadyAdded) { throw new ServiceException("Can not add two specific resource to the same room"); } } var usedResourcesQuantity = await _applicationContext.RoomResources .Where(x => x.ResourceId == command.ResourceId) .SumAsync(x => x.Quantity); if (usedResourcesQuantity + command.Quantity > resource.TotalQuantity) { throw new ServiceException("There is no available resource"); } RoomResource roomResource = _mapper.Map <RoomResource>(command); await _applicationContext.RoomResources.AddAsync(roomResource); await _applicationContext.SaveChangesAsync(); }
public async Task <IActionResult> AddResourceToRoomAsync([FromBody] AddResourceToRoomCommand command) { await _roomBusiness.AddResourceAsync(command); return(Ok()); }