public async Task <CommandResult> ExecuteAsync(int imageId) { try { CsmsProductsPhoto photo = await _productPhotoRepository.GetByIdAsync(imageId); if (photo == null) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotFound, Description = Message.NotFound })); } else { await _productPhotoRepository.DeleteAsync(photo); return(CommandResult.Success); } } catch (Exception ex) { Logging <DeleteProductPhotoCommand> .Error(ex, "ImageId: " + imageId); return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.InternalServerError, Description = Message.InternalServerError })); } }
public async Task <CommandResult> ExecuteAsync(int imageId, IFormFile file) { try { if (file == null) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotAcceptable, Description = "File is null" })); } if (file.Length < 50000 || file.Length > 4000000) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotAcceptable, Description = "File size must to beetween 50kb and 4Mb" })); } if (!file.ContentType.Contains("image/")) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotAcceptable, Description = "File not support" })); } var names = file.FileName.Split('.'); var fileExtend = names[names.Length - 1]; byte[] fileContent; using (Stream stream = file.OpenReadStream()) { using (var binaryReader = new BinaryReader(stream)) { fileContent = binaryReader.ReadBytes((int)file.Length); } } CsmsProductsPhoto entity = await _productPhotoRepository.GetByIdAsync(imageId) ?? new CsmsProductsPhoto(); entity.FileSize = file.Length.ToString(); entity.FileType = file.ContentType; entity.Picture = fileContent; entity.Filename = DateTime.Now.ToString(DateFormat.DateTimeFormatyyyyMMdd_hhmmss) + "." + fileExtend; if (entity.Id == 0) { await _productPhotoRepository.InsertAsync(entity); } else { await _productPhotoRepository.UpdateAsync(entity); } return(CommandResult.SuccessWithData(new UploadPhotoSuccessViewModel(entity.Id))); } catch (Exception ex) { Logging <UploadProductPhotoCommand> .Error(ex); return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.InternalServerError, Description = Message.InternalServerError })); } }