Lab 6 то что нужно было сделать с комментами

This commit is contained in:
Павел Сорокин 2023-05-10 11:25:59 +04:00
parent 6da86fa98c
commit 99a8de13ba
5 changed files with 37 additions and 21 deletions

View File

@ -24,7 +24,7 @@ export default function Post(props) {
const refresh = async function(id,text)
const refresh = async function(id,user,text)
{
const requestParams={
method:"PUT",
@ -33,7 +33,7 @@ export default function Post(props) {
"Authorization": getTokenForHeader(),
}
};
const response=await fetch(`http://localhost:8080/api/1.0/comment/${id}?Text=${text}`,requestParams);
const response=await fetch(`http://localhost:8080/api/1.0/comment/${id}/curUser/${userId}/commentUser/${user}?Text=${text}`,requestParams);
return await response.json();
}
@ -81,7 +81,7 @@ export default function Post(props) {
const remove = async function(id){
const remove = async function(id,user){
const requestParams = {
method: "DELETE",
headers:{
@ -89,7 +89,7 @@ export default function Post(props) {
"Authorization": getTokenForHeader(),
}
};
const requestUrl = `http://localhost:8080/api/1.0/post/${ad.id}/Comment/${id}`;
const requestUrl = `http://localhost:8080/api/1.0/post/${ad.id}/Comment/${id}/curUser/${userId}/commentUser/${user}`;
const response=await fetch(requestUrl,requestParams);
return await response.json;
};
@ -114,9 +114,9 @@ export default function Post(props) {
const [comments,setComments] = useState([]);
const rem_but = function(id,event)
const rem_but = function(id,user,event)
{
remove(id).then((result)=>{
remove(id,user).then((result)=>{
getAll();
});
}
@ -129,15 +129,14 @@ export default function Post(props) {
create(commentInput.value, userId).then((result) => {
getAll();
commentInput.value = "";
alert(`Comment[id=${result.id}, text=${result.firstName}]`);
});
}
const edit_btn = function(id,event)
const edit_btn = function(id,user,event)
{
console.log("Обновление")
refresh(id, commentInput.value).then((result)=>{
refresh(id,user, commentInput.value).then((result)=>{
getAll();
commentInput.value = "";
});

View File

@ -1,10 +1,10 @@
export default function TableComment(props) {
function edit(id,e) {
props.onEdit(id);
function edit(id,user,e) {
props.onEdit(id,user);
}
function remove(id,e) {
props.onRemove(id,e);
function remove(id,user,e) {
props.onRemove(id,user,e);
}
@ -24,8 +24,8 @@ export default function TableComment(props) {
{console.log(comment.user)}
<th scope="row">{comment.user}</th>
<td>{comment.text}</td>
<td><button type="button" className="btn btn-outline-light text-center mx-2" onClick={(e) => edit(comment.id, e)}><i className="fa-sharp fa-solid fa-pen"></i></button></td>
<td><button type="button" className="btn btn-outline-light text-center mx-2" onClick={(e) => remove(comment.id, e)}><i className="fa-sharp fa-solid fa-trash"></i></button></td>
<td><button type="button" className="btn btn-outline-light text-center mx-2" onClick={(e) => edit(comment.id,comment.userId, e)}><i className="fa-sharp fa-solid fa-pen"></i></button></td>
<td><button type="button" className="btn btn-outline-light text-center mx-2" onClick={(e) => remove(comment.id,comment.userId,e)}><i className="fa-sharp fa-solid fa-trash"></i></button></td>
</tr>
))}
</tbody>

View File

@ -5,6 +5,7 @@ import ru.ulstu.is.sbapp.Comment.service.CommentService;
import ru.ulstu.is.sbapp.Configuration.OpenAPI30Configuration;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping(OpenAPI30Configuration.API_PREFIX + "/comment")
@ -28,10 +29,16 @@ public class CommentController {
return new CommentDto(commentService.addComment(Text));
}
@PutMapping("/{id}")
@PutMapping("/{id}/curUser/{userId}/commentUser/{userComId}")
public CommentDto updateComment(@PathVariable Long id,
@PathVariable Long userId,
@PathVariable Long userComId,
@RequestParam("Text") String Text){
return new CommentDto(commentService.updateComment(id,Text));
if(Objects.equals(userComId, userId))
{
return new CommentDto(commentService.updateComment(id,Text));
}
return new CommentDto(commentService.findComment(id));
}
@DeleteMapping("/{id}")
public CommentDto deleteComment(@PathVariable Long id) {

View File

@ -8,19 +8,22 @@ public class CommentDto {
private Long id;
private String Text;
private String userName;
private Long userId;
public CommentDto(){}
public CommentDto(Comment comment)
{
this.id= comment.getId();
this.Text=comment.getText();
this.userName=comment.getUser().getLogin();
this.userId=comment.getUser().getId();
}
public Long getId()
{
return id;
}
public String getText() {return Text;}
public Long getUserId(){return userId;}
public String getUser()
{
return userName;

View File

@ -10,6 +10,7 @@ import ru.ulstu.is.sbapp.User.controller.UserDto;
import ru.ulstu.is.sbapp.User.service.UserService;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping(OpenAPI30Configuration.API_PREFIX + "/post")
@ -50,11 +51,17 @@ public class PostController {
@RequestParam("Text") String Text) {
postService.addCommentToPost(id, userId,Text);
}
@DeleteMapping("/{id}/Comment/{commentId}")
@DeleteMapping("/{id}/Comment/{commentId}/curUser/{userId}/commentUser/{userComId}")
public void removeComment(@PathVariable Long id,
@PathVariable Long commentId)
@PathVariable Long commentId,
@PathVariable Long userId,
@PathVariable Long userComId)
{
postService.removeCommentFromPost(id,commentId);
if(Objects.equals(userComId, userId))
{
postService.removeCommentFromPost(id,commentId);
}
}