예제 #1
0
        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();
        }
예제 #2
0
        public async Task <IActionResult> AddResourceToRoomAsync([FromBody] AddResourceToRoomCommand command)
        {
            await _roomBusiness.AddResourceAsync(command);

            return(Ok());
        }