示例#1
0
        public async Task <IActionResult> UpdateLocationItem([FromBody] LocationItemDto locationItemDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var locationItem = await _repository.GetByIdAsync(locationItemDto.LocationId, locationItemDto.ItemId);

            if (locationItem == null)
            {
                return(NotFound("Location Item does not exist"));
            }

            _mapper.Map(locationItemDto, locationItem);

            try
            {
                _repository.Update(locationItem);
                await _unitOfWork.SaveAsync();
            }

            catch (Exception)
            {
                throw new Exception("An unexpected error occured. Could not update.");
            }

            return(Ok(_mapper.Map <LocationItemDto>(locationItem)));
        }
示例#2
0
        public async Task <IActionResult> CreateLocationItem([FromBody] LocationItemDto locationItemDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var locationItem = _mapper.Map <LocationItem>(locationItemDto);

            try
            {
                _repository.Add(locationItem);
                await _unitOfWork.SaveAsync();

                var createdResult = CreatedAtAction("GetLocationItemById", new { locationId = locationItem.LocationId, itemId = locationItem.ItemId }, null);
                createdResult.StatusCode = 200;
                return(createdResult);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }