public async Task <Domain.Photo> Handle(Command request, CancellationToken cancellationToken) { var photoId = await _photoAccesor.UploadPhoto(request.File); if (photoId == "Empty" || photoId == "Error") { throw new Exception("Error submitting file"); } var user = await _dataContext.Users.FirstOrDefaultAsync(usr => usr.UserName == _userAccesor.GetUserName()); var photo = new Domain.Photo() { Id = photoId, IsMain = (user.Photos.Count > 0) ? false : true, AppUserId = user.Id }; await _dataContext.Photos.AddAsync(photo); if (await _dataContext.SaveChangesAsync() > 0) { return(photo); } throw new Exception("Error saving changes"); }
public async Task <Photo> Handle(Command request, CancellationToken cancellationToken) { var UploadPhotoResult = photoAccessor.UploadPhoto(request.File); var user = await _context.Users.SingleOrDefaultAsync(p => p.UserName == userAccessor.GetCurrentUsername()); var photo = new Photo { Id = UploadPhotoResult.PublicId, Url = UploadPhotoResult.Url.ToString() }; if (!user.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(photo); } throw new Exception("Problem in add Photo handler"); }