public static CommentImage CommentImageDTO_To_CommentImage(CommentImageDTO commentImageDTO) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <CommentImageDTO, CommentImage>()).CreateMapper(); CommentImage commentImage = mapper.Map <CommentImageDTO, CommentImage>(commentImageDTO); return(commentImage); }
public static CommentEntity CommentImageDTO_To_CommentEntity(CommentImageDTO commentImageDTO) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <CommentImageDTO, CommentEntity>()).CreateMapper(); CommentEntity commentEntity = mapper.Map <CommentImageDTO, CommentEntity>(commentImageDTO); return(commentEntity); }
public async Task <IActionResult> Add([FromBody] CommentImageDTO commentImageDTO) { if (ModelState.IsValid) { string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; try { string commentId = _commentRepo.Add(commentImageDTO.Content, userId); _commentRepo.Save(); if (String.IsNullOrEmpty(commentId)) { _imageCommentRepo.CommentImage(commentId, commentImageDTO.ImageId); return(Ok()); } return(NoContent()); } catch (Exception ex) { throw new Exception(ex.Message); } } else { return(BadRequest()); } }
public static CommentImageDTO CommentImagePostViewModel_To_CommentImageDTO(CommentImagePostViewModel commentImagePostViewModel) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <CommentImagePostViewModel, CommentImageDTO>()).CreateMapper(); CommentImageDTO commentImageDTO = mapper.Map <CommentImagePostViewModel, CommentImageDTO>(commentImagePostViewModel); return(commentImageDTO); }
public void PostImageComment(CommentImageDTO commentImageDTO) { CommentEntity commentEntity = MapperModule.CommentImageDTO_To_CommentEntity(commentImageDTO); Comment comment = MapperModule.CommentImageDTO_To_Comment(commentImageDTO); CommentImage commentImage = MapperModule.CommentImageDTO_To_CommentImage(commentImageDTO); comment.commentEntity = commentEntity; comment.commentImage = commentImage; Database.Comments.Create(comment); Database.Save(); }
public string postImageComment(CommentImagePostViewModel postImageComment) { var file = this.Request.Files[0]; if (file.ContentType == "image/jpeg" || file.ContentType == "image/png") { if (file.ContentLength <= Int32.Parse(ConfigurationManager.AppSettings["MaxFileSize"])) { CommentImageDTO commentImageDTO = MapperModule.CommentImagePostViewModel_To_CommentImageDTO(postImageComment); commentImageDTO.userID = this.User.Identity.GetUserId(); commentImageDTO.contentLength = file.ContentLength; commentImageDTO.contentType = file.ContentType; commentImageDTO.fileName = file.FileName; byte[] data = new byte[file.ContentLength]; file.InputStream.Read(data, 0, file.ContentLength); commentImageDTO.data = data; try { commentService.PostImageComment(commentImageDTO); } catch (DbEntityValidationException ex) { logger.Error(ex, "Comment not saved to the Database validation of entities fails"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } catch (DbUpdateException ex) { logger.Error(ex, "Comment not saved to the Database"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } catch (DataException ex) { logger.Error(ex, "Comment not saved to the Database"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } catch (SystemException ex) { logger.Error(ex, "Exeption occured when user post new comment"); return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = ex.Message })); } string imageBase64Data = Convert.ToBase64String(data); string imageDataURL = string.Format("data:{0};base64,{1}", file.ContentType, imageBase64Data); return(JsonConvert.SerializeObject(new { id = 10, content = "", file_url = imageDataURL, file_mime_type = file.ContentType })); } else { return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = "File is too big, you can attach file whith size less than 5000 bytes" })); } } else { return(JsonConvert.SerializeObject(new ErrorResponse { success = false, message = "You can attach only photo in jpeg and png" })); } }