示例#1
0
        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> Delete(int Id)
        {
            var checkByIdFromRepo = await userPotoDal.GetAsync(x => x.Id == Id);

            if (checkByIdFromRepo == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.NotFound });
            }

            var deleteFileFromFolder = await upload.DeleteFile(checkByIdFromRepo.Name, "userprofile");

            await userPotoDal.Delete(checkByIdFromRepo);

            return(mapper.Map <UserPhoto, UserPhotoForReturnDto>(checkByIdFromRepo));
        }