public async Task <PhotoForProductDto> SetMainPhotoForProductAsync(PhotoForProductDto photo) { var currentMain = await _context.Photos .Where(p => p.IsMain == true && p.ProductId == photo.ProductId) .FirstOrDefaultAsync(); var photoToSet = await _context.Photos .FirstOrDefaultAsync(p => p.PhotoId == photo.PhotoId); if (photoToSet != null) { if (currentMain != null) { currentMain.IsMain = false; } photoToSet.IsMain = true; } return(photoToSet.Adapt <PhotoForProductDto>()); }
public async Task <ActionResult <PhotoForProductDto> > SetMainPhotoForProduct(PhotoForProductDto photo) { var photoToSet = await _repository.SetMainPhotoForProductAsync(photo); if (photoToSet == null) { return(NotFound()); } await _repository.CommitAsync(); return(photoToSet); }