public async Task <bool> CreateAsync(FarmPicturesModifyRequest request) { var index = 0; foreach (var picture in request.Pictures) { var binaryPicture = ImageUtil.EncodeJavascriptBase64(picture.Base64Data); var pictureData = Convert.FromBase64String(binaryPicture); var pictureId = _pictureRepository.AddWithInt64Entity(new Picture() { CreatedById = request.UpdatedById, CreatedDate = request.CreatedDate, FileName = picture.FileName, MimeType = picture.ContentType, UpdatedById = request.UpdatedById, UpdatedDate = request.UpdatedDate, BinaryData = pictureData, StatusId = PictureStatus.Pending.GetCode() }); var farmPictureType = index == 0 ? (int)FarmPictureType.Thumbnail : (int)FarmPictureType.Secondary; await _farmPictureRepository.AddAsync(new FarmPicture() { FarmId = request.FarmId, PictureId = pictureId, PictureTypeId = farmPictureType }); index += 1; } return(true); }
public async Task <bool> UpdateStatusByFarmIdAsync(FarmPicturesModifyRequest request, PictureStatus pictureStatus) { await(from farmPicture in _farmPictureRepository.Get(x => x.FarmId == request.FarmId) join picture in _pictureRepository.Table on farmPicture.PictureId equals picture.Id select picture) .Set(x => x.StatusId, pictureStatus.GetCode()) .Set(x => x.UpdatedById, request.UpdatedById) .Set(x => x.UpdatedDate, DateTimeOffset.UtcNow) .UpdateAsync(); return(true); }
public async Task <bool> UpdateAsync(FarmPicturesModifyRequest request) { var pictureIds = request.Pictures.Select(x => x.Id); var deleteFarmPictures = _farmPictureRepository .Get(x => x.FarmId == request.FarmId && x.PictureId.NotIn(pictureIds)); var deletePictureIds = deleteFarmPictures.Select(x => x.PictureId).ToList(); if (deletePictureIds.Any()) { await deleteFarmPictures.DeleteAsync(); await _pictureRepository.Get(x => x.Id.In(deletePictureIds)) .DeleteAsync(); } var pictureTypeId = (int)FarmPictureType.Thumbnail; var shouldAddPicture = true; var hasPicture = _farmPictureRepository.Get(x => x.FarmId == request.FarmId && x.PictureTypeId == pictureTypeId).Any(); if (hasPicture) { shouldAddPicture = false; } foreach (var picture in request.Pictures) { if (!string.IsNullOrEmpty(picture.Base64Data)) { var base64Data = ImageUtil.EncodeJavascriptBase64(picture.Base64Data); var pictureData = Convert.FromBase64String(base64Data); var pictureId = _pictureRepository.AddWithInt64Entity(new Picture() { CreatedById = request.UpdatedById, CreatedDate = request.CreatedDate, FileName = picture.FileName, MimeType = picture.ContentType, UpdatedById = request.UpdatedById, UpdatedDate = request.UpdatedDate, BinaryData = pictureData, StatusId = PictureStatus.Pending.GetCode() }); var farmPictureType = shouldAddPicture ? pictureTypeId : (int)FarmPictureType.Secondary; _farmPictureRepository.Add(new FarmPicture() { FarmId = request.FarmId, PictureId = pictureId, PictureTypeId = farmPictureType }); shouldAddPicture = false; } } var firstRestPicture = await _farmPictureRepository.FirstOrDefaultAsync(x => x.FarmId == request.FarmId && x.PictureTypeId != pictureTypeId); if (firstRestPicture != null) { firstRestPicture.PictureTypeId = pictureTypeId; await _farmPictureRepository.UpdateAsync(firstRestPicture); } return(true); }