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); } }
public async Task <ActionResult <DTOs.Label> > GetAsync(int id) { try { var label = await _labelRepository.GetLabelAsync(id); var labelDTO = _mapper.Map <DTOs.Label>(label); return(Ok(labelDTO)); } catch (Exception) { var msg = "Error occurred while retrieving data from database."; _logger.LogError(msg); return(StatusCode(StatusCodes.Status500InternalServerError, msg)); } }