public dynamic Create(PostModel postModel) { string accessToken = Request.Headers[HeaderNames.Authorization]; string token = accessToken.Split("Bearer ")[1]; UserReadDTO userRead = TokenUtil.GetSubFromToken(token); if (ModelState.IsValid) { PostWriteDTO postWrite = new PostWriteDTO { TitlePost = postModel.Title, SummaryPost = postModel.Summary, ContentPost = postModel.Content, EncodeImage = postModel.EncodeImage, UserID = userRead.UserID }; if (_postService.Create(postWrite).Result.status) { return(new { status = true, code = ReturnCodes.DataCreateSucceeded, message = "Thêm Post Thành Công" }); } } return(new { status = false, code = ReturnCodes.DataCreateFailed, message = "Có Lỗi Xảy Ra" }); }
public async Task <IActionResult> createPostAsync(PostWriteDTO model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var postData = new posts(); postData.user_id = model.user_Id; postData.message = model.message; postData.created_at = DateTime.Now; postData.updated_at = null; uow.Post.Create(postData); await uow.save(); return(Ok(new { success = true })); } catch (Exception ex) { return(Ok(ex.Message)); } }
public void CreatPost_NullParameter_ActualFalse() { //Arrange PostWriteDTO postWriteDTO = null; //Act var result = postService.Create(postWriteDTO).Result; //Assert Assert.AreEqual(false, result.status); }
public void UpdatePost_NullParameter_ActualFalse() { //Arrange string UserID = null; PostWriteDTO postWriteDTO = null; //Act var result = postService.Update(postWriteDTO, UserID).Result; //Assert Assert.AreEqual(false, result.status); }
public void UpdatePost_NullPostWriteDTO_ActualFalse() { //Arrange string UserID = Guid.NewGuid().ToString(); PostWriteDTO postWriteDTO = null; //Act var result = postService.Update(postWriteDTO, UserID).Result; //Assert Assert.AreEqual(false, result.status); }
/*Thêm Một Post Mới*/ public async Task <CustomResponse> Create(PostWriteDTO postWrite) { if (postWrite == null) { return(new CustomResponse(false, "Post is Null")); } if (postWrite.isPropertiesNull()) { return(new CustomResponse(false, "Post properties is Null")); } if (postWrite.isPropertiesEmpty()) { return(new CustomResponse(false, "Post properties is Empty")); } try { //Init a Post Post post = new Post(); post.TitlePost = postWrite.TitlePost; post.SummaryPost = postWrite.SummaryPost; post.ContentPost = postWrite.ContentPost; post.DateCreate = DateTime.Now; post.DateUpdate = DateTime.Now; post.UserID = new Guid(postWrite.UserID); //Init ID cho Image Guid NewImageID = Guid.NewGuid(); post.ImageID = NewImageID; //Init a Image ImageGallery image = new ImageGallery(); image.Base64Code = postWrite.EncodeImage; image.ImageID = NewImageID; bool task_add_image = db.ImageGalleries.AddAsync(image).IsCompleted; if (task_add_image) { await db.Posts.AddAsync(post); await db.SaveChangesAsync(); } } catch (Exception e) { return(new CustomResponse(false, e.Message)); } return(new CustomResponse(true, "Thêm Post Thành Công")); }
public void CreatPost_TitleNull_ActualFalse() { //Arrange PostWriteDTO postWriteDTO = new PostWriteDTO { TitlePost = null, SummaryPost = "Summary", ContentPost = "Content", DateCreated = DateTime.Now, EncodeImage = ImageGalleryTest.Base64Code, UserID = UserTest.UserID.ToString(), ImageID = ImageGalleryTest.ImageID.ToString(), }; //Act var result = postService.Create(postWriteDTO).Result; //Assert Assert.AreEqual(false, result.status); }
public void UpdatePost_UserIDValueEmpty_ActualFalse() { //Arrange string UserID = " "; PostWriteDTO postWriteDTO = new PostWriteDTO { TitlePost = "Tiltle", SummaryPost = "Summary", ContentPost = "Content", DateCreated = DateTime.Now, EncodeImage = ImageGalleryTest.Base64Code, UserID = UserTest.UserID.ToString(), ImageID = ImageGalleryTest.ImageID.ToString(), }; //Act var result = postService.Update(postWriteDTO, UserID).Result; //Assert Assert.AreEqual(false, result.status); }
public void UpdatePost_UserNotExistNotUpdateImage_ActualTrue() { //Arrange db.Posts.RemoveRange(db.Posts); db.SaveChanges(); Post post = new Post { TitlePost = "Title", SummaryPost = "Summary", ContentPost = "Content", DateCreate = DateTime.Now, UserID = UserTest.UserID, ImageID = ImageGalleryTest.ImageID, }; db.Posts.Add(post); db.SaveChanges(); Post PostTest = db.Posts.First(); string UserID = UserTest.UserID.ToString(); PostWriteDTO postWriteDTO = new PostWriteDTO { PostID = PostTest.PostID, TitlePost = "Update Title", SummaryPost = "Update Summary", ContentPost = "Content", DateCreated = DateTime.Now, EncodeImage = "", UserID = UserTest.UserID.ToString(), ImageID = ImageGalleryTest.ImageID.ToString(), }; //Act var result = postService.Update(postWriteDTO, UserID).Result; //Assert Assert.AreEqual(true, result.status); }
public dynamic Update([FromRoute] int PostID, [FromBody] PostModel postModel) { string accessToken = Request.Headers[HeaderNames.Authorization]; string token = accessToken.Split("Bearer ")[1]; UserReadDTO userRead = TokenUtil.GetSubFromToken(token); PostWriteDTO postWrite = new PostWriteDTO { PostID = PostID, TitlePost = postModel.Title, ContentPost = postModel.Content, SummaryPost = postModel.Summary, EncodeImage = postModel.EncodeImage, UserID = userRead.UserID }; Task <CustomResponse> remove_task = _postService.Update(postWrite, userRead.UserID); return(new { status = remove_task.Result.status, code = (remove_task.Result.status ? ReturnCodes.DataRemoveSucceeded : ReturnCodes.DataRemoveFailed), message = remove_task.Result.message }); }
/*Cập Nhật Một Bài Post*/ public async Task <CustomResponse> Update(PostWriteDTO postWrite, string UserID) { if (postWrite == null) { return(new CustomResponse(false, "Post is Null")); } postWrite.UserID = UserID; if (postWrite.isPropertiesNullWithoutEndcode()) { return(new CustomResponse(false, "Post properties is Null")); } if (postWrite.isPropertiesEmptyWithoutEndcode()) { return(new CustomResponse(false, "Post properties is Empty")); } try { Post CurPost = db.Posts.FirstOrDefaultAsync(p => p.PostID == postWrite.PostID).Result; //Tìm Thấy Post Cần Cập Nhật if (CurPost != null) { //Fix Post Không Chính Chủ if (!CurPost.UserID.Equals(new Guid(UserID))) { return(new CustomResponse(false, "Bạn Không Có Quyền Cập Nhật Post Này")); } //Có Cập Nhật Hình Ảnh if (postWrite.EncodeImage.Length > 0) { ImageGallery new_image = new ImageGallery { ImageID = Guid.NewGuid(), Base64Code = postWrite.EncodeImage }; bool add_image_task = db.ImageGalleries.AddAsync(new_image).IsCompleted; if (add_image_task) { CurPost.TitlePost = postWrite.TitlePost; CurPost.SummaryPost = postWrite.SummaryPost; CurPost.ContentPost = postWrite.ContentPost; CurPost.DateUpdate = DateTime.Now; CurPost.ImageID = new_image.ImageID; } else { return(new CustomResponse(false, "Không Thể Cập Nhật Hình Ảnh Vào Lúc Này")); } } //Không Cập Nhật Hình Ảnh else { CurPost.TitlePost = postWrite.TitlePost; CurPost.SummaryPost = postWrite.SummaryPost; CurPost.ContentPost = postWrite.ContentPost; CurPost.DateUpdate = DateTime.Now; } //Done and Save Change All Modifies await db.SaveChangesAsync(); } //Tìm Không Thấy Post Cần Cập Nhật else { return(new CustomResponse(false, "Post Không Tồn Tại")); } } catch (Exception e) { return(new CustomResponse(false, e.Message)); } return(new CustomResponse(true, "Cập Nhật Thành Công")); }