public async Task <UserPhotoForReturnDto> MakeMainPhotoAsync(UserPhotoForCreationDto creationDto, int userId) { var claimId = int.Parse(httpContextAccessor.HttpContext.User?.Claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value); if (claimId != userId) { throw new RestException(HttpStatusCode.BadRequest, new { AlreadyExist = Messages.OperationDenied }); } var checkByIdFromRepo = await userPhotoDal.GetAsync(x => x.Id == creationDto.Id); if (checkByIdFromRepo == null) { throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.NotFound }); } var mainPhotoFromRepo = await userPhotoDal.GetAsync(x => x.IsMain == true && x.UserId == creationDto.UserId); if (mainPhotoFromRepo != null) { mainPhotoFromRepo.IsMain = false; await userPhotoDal.Update(mainPhotoFromRepo); } var mapForUpdate = mapper.Map(creationDto, checkByIdFromRepo); var updatePhoto = await userPhotoDal.Update(mapForUpdate); return(mapper.Map <UserPhoto, UserPhotoForReturnDto>(updatePhoto)); }
public async Task <UserPhotoForReturnDto> Update(UserPhotoForCreationDto updateDto) { var checkByIdFromRepo = await userPotoDal.GetAsync(x => x.Id == updateDto.UserId); if (checkByIdFromRepo == null) { throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.NotFound }); } var mapForUpdate = mapper.Map(updateDto, checkByIdFromRepo); var updatePhoto = await userPotoDal.Update(mapForUpdate); return(mapper.Map <UserPhoto, UserPhotoForReturnDto>(updatePhoto)); }