/// <summary> /// Calls WebApi to get Dog Album for single dog. /// </summary> /// <param name="id">Dog Id <see cref="int" /></param> public async Task GetDogAlbumImages(int id) { // show animation and initialize state properties loading = true; NewDogAlbum(); // get dog album HttpResponseMessage response = await _http.GetAsync($"api/DogAlbum/{id}"); if (response.IsSuccessStatusCode) { // set dog album into state Album = await response.Content.ReadFromJsonAsync <DogAlbumImages>(); loading = false; // notify subscribers state has changed if (Album != null) { NotifyStateChanged(); } } else if (response.StatusCode == HttpStatusCode.Unauthorized) { // user is not authorized to edit this dog album (likely not owner) _notification.DisplayMessage(NotificationType.NotAuthorizedOwnerEditError); _navigate.ToAllDoggos(); return; } }
public async Task <IActionResult> Put(int id, DogAlbumImages album) { if (id != album.DogId) { return(BadRequest()); } // ensure user attempting to update biography is the dog owner string dogOwnerId = await _userService.GetOwnerIdByDogId(album.DogId); string requestUser = GetUserId(); if (dogOwnerId == requestUser) { bool success = await _service.UpdateDogAlbumImages(album, requestUser); if (success) { return(Ok()); } else { _logger.LogError($"Failed to save Dog Album for dog id {album.DogId} by {requestUser}"); } return(BadRequest()); } else { // unauthorized: user attempting to update dog is not the owner _logger.LogWarning($"Request user ({requestUser}) does not have permission (non-owner) to update dog album for dog id {album.DogId}"); return(Unauthorized()); } }
public async Task <ActionResult <DogAlbumImages> > Get(int id) { // ensure user making request for dog album is actual dog owner string ownerId = await _userService.GetOwnerIdByDogId(id); string requestUser = GetUserId(); if (ownerId != requestUser) { // unauthorized: user attempting to get dog is not the owner _logger.LogWarning($"Request user ({requestUser}) does not have permission (non-owner) to update dog album for dog id {id}"); return(Unauthorized()); } DogAlbumImages album = await _service.GetDogAlbumImages(id); if (album != null) { return(Ok(album)); } else { return(NotFound()); } }
/// <summary> /// Update Dog Album images (adds any new images, removes any deleted images that are already saved) /// </summary> /// <param name="dogAlbum"><see cref="DogAlbumImages"/> object with updated images</param> /// <param name="userId">User Id <see cref="string"/> for current request user</param> /// <returns>success <see cref="bool"/></returns> public async Task <bool> UpdateDogAlbumImages(DogAlbumImages dogAlbum, string userId) { if (dogAlbum.Images.Count() > 0) { try { // separate image groups, ignore previously saved unchanged images // new image group for saving new images IEnumerable <AlbumImage> newImages = dogAlbum.Images .Where(i => i.Id == 0 && !(i.Delete ?? false)); // and list of image Id's to soft delete List <int> imageIdsToDelete = dogAlbum.Images .Where(i => (i.Delete ?? false) && i.Id != 0) .Select(i => i.Id) .ToList(); // save new images if (newImages.Count() > 0) { await SaveNewAlbumImages(newImages, dogAlbum.DogId, userId); } // soft delete any images flagged for deletion if (imageIdsToDelete.Count > 0) { await _repository.SoftDeleteImageRange(imageIdsToDelete); } return(true); } catch (Exception ex) { _logger.LogError(ex, "Image Service error while updating Dog Album Images."); return(false); } } // no dog album images, return true return(true); }
/// <summary> /// Initializes new <see cref="DogAlbumImages"/> instance in state. /// </summary> public void NewDogAlbum() => Album = new DogAlbumImages();