コード例 #1
0
        public async Task<ActionResult<DTOs.Photo>> PutAsync(int id, [FromBody] DTOs.Photo photo)
        {
            try
            {
                if (id != photo.Id)
                    return BadRequest($"Photo id from url and body are not identical.");

                if (await _photoRepository.GetPhotoAsync(id) == null)
                    return NotFound($"Photo with id '{id}' not found.");

                // Check label's id
                if (photo.Labels != null)
                {
                    foreach (var l in photo.Labels)
                    {
                        var label = await _labelRepository.GetLabelAsync(l.Id);
                        if (label == null)
                            return BadRequest($"Label with id '{l.Id}' not found!");
                    }
                }

                // Update photo
                var photoUpdated = await _photoRepository.UpdatePhotoAsync(_mapper.Map<Models.Photo>(photo), true);
                var photoDTO = _mapper.Map<DTOs.Photo>(photoUpdated);
                return Ok(photoDTO);
            }
            catch (Exception)
            {
                var msg = "Error occurred while updating data of database.";
                _logger.LogError(msg);
                return StatusCode(StatusCodes.Status500InternalServerError, msg);
            }
        }
コード例 #2
0
 public async Task<ActionResult<DTOs.Photo>> PostAsync([FromBody] DTOs.Photo photo)
 {
     try
     {
         if (photo == null)
             return BadRequest($"Photo object from body is null.");
         // Create photo
         var (userId, userRole) = GetCurrentUserInfo();
         var photoModel = _mapper.Map<Models.Photo>(photo);
         photoModel.UserId = userId;
         var createdPhoto = await _photoRepository.AddPhotoAsync(photoModel);
         // Get created photo
         var photoDTO = _mapper.Map<DTOs.Photo>(await _photoRepository.GetPhotoAsync(createdPhoto.Id));
         return CreatedAtRoute("GetPhoto", new { id = photoDTO.Id }, photoDTO);
     }
     catch (Exception)
     {
         var msg = "Error occurred while creating new photo into database.";
         _logger.LogError(msg);
         return StatusCode(StatusCodes.Status500InternalServerError, msg);
     }
 }