7.8: Setting Main Photo in the API
Adding SetMainPhotoForUser() Service Method
Task<Photo> GetMainPhotoForUser(int userId);
Task<bool> SetMainPhotoForUser(int userId, PhotoForReturn photo);public async Task<Photo> GetMainPhotoForUser(int userId)
{
return await _context.Photos.Where(u => u.UserId == userId).FirstOrDefaultAsync(p => p.IsMain);
}
public async Task<bool> SetMainPhotoForUser(int userId, PhotoForReturn photo)
{
var currentMainPhoto = await GetMainPhotoForUser(userId);
if (currentMainPhoto != null)
currentMainPhoto.IsMain = false;
_context.Photos.FirstOrDefault(p => p.Id == photo.Id).IsMain = true;
return await SaveAll();
}Adding SetMainPhoto() Method to the PhotosController
Test in Postman



Last updated