add friend resources

This commit is contained in:
Valentin CZERYBA 2023-01-15 21:53:42 +01:00
parent c767a448e8
commit 981ed99434
2 changed files with 28 additions and 68 deletions

View File

@ -12,7 +12,7 @@ public class FriendEntity extends PanacheMongoEntity {
public String owner; public String owner;
public Collection<String> friends; public Collection<String> friends;
public Short status;
public LocalDateTime created_at; public LocalDateTime created_at;
public LocalDateTime updated_at; public LocalDateTime updated_at;

View File

@ -155,22 +155,22 @@ public class FriendRessources {
@RolesAllowed({"Admin", "User"}) @RolesAllowed({"Admin", "User"})
@Path("{id}") @Path("{id}")
@Transactional @Transactional
public Response createGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, GroupEntity group) { public Response createGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, FriendEntity friend) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid); UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user); Response.Status status = getResponseCheck(ctx, userCookie, user);
GroupEntity newGroup = new GroupEntity(); FriendEntity newFriend = new FriendEntity();
if (status.equals(Response.Status.OK)) { if (status.equals(Response.Status.OK)) {
newGroup.name = group.name; newFriend.owner = friend.owner;
newGroup.admins = group.admins; newFriend.friends = friend.friends;
newGroup.created_at = LocalDateTime.now(); newFriend.created_at = LocalDateTime.now();
newGroup.updated_at = LocalDateTime.now(); newFriend.updated_at = LocalDateTime.now();
newGroup.persist(); newFriend.persist();
status = Response.Status.CREATED; status = Response.Status.CREATED;
} }
return Response.status(status).entity(newGroup).build(); return Response.status(status).entity(newFriend).build();
} }
@ -185,41 +185,20 @@ public class FriendRessources {
UsersEntity user = UsersEntity.findById(kid); UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user); Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) { if (status.equals(Response.Status.OK)) {
GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult(); FriendEntity singleFriend = FriendEntity.find("id", new ObjectId(id)).firstResult();
if (singleGroup == null) { if (singleFriend == null) {
status = Response.Status.NOT_FOUND; status = Response.Status.NOT_FOUND;
} else { } else {
singleGroup.status = -1; singleFriend.status = -1;
singleGroup.updated_at = LocalDateTime.now(); singleFriend.updated_at = LocalDateTime.now();
singleGroup.deleted_at = LocalDateTime.now(); singleFriend.deleted_at = LocalDateTime.now();
singleGroup.persist(); singleFriend.persist();
} }
} }
return Response.status(status).build(); return Response.status(status).build();
} }
@DELETE
@Path("/disable/{id}")
@RolesAllowed({"Admin", "User"})
@Transactional
public Response changeStatusSingleGroupToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
@PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
GroupEntity singleGroup = GroupEntity.find("id", new ObjectId(id)).firstResult();
if (singleGroup == null) {
status = Response.Status.NOT_FOUND;
} else {
singleGroup.status = 0;
singleGroup.updated_at = LocalDateTime.now();
singleGroup.deleted_at = LocalDateTime.now();
singleGroup.persist();
}
}
return Response.status(status).build();
}
// PATCH // PATCH
@PATCH @PATCH
@ -228,27 +207,28 @@ public class FriendRessources {
@Transactional @Transactional
@Path("{id}") @Path("{id}")
public Response updateGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, public Response updateGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
GroupEntity group, @PathParam("id") String id) { FriendEntity friend, @PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid); UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user); Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) { if (status.equals(Response.Status.OK)) {
GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id)); FriendEntity friendOrig = FriendEntity.findById(new ObjectId(id));
if (groupOrig == null) { if (friendOrig == null) {
status = Response.Status.NOT_FOUND; status = Response.Status.NOT_FOUND;
} else { } else {
groupOrig.name = group.name; friendOrig.owner = friend.owner;
friendOrig.friends = friend.friends;
groupOrig.updated_at = LocalDateTime.now(); friendOrig.updated_at = LocalDateTime.now();
if(group.status == 1){ if(friend.status == 1){
groupOrig.deleted_at = null; friendOrig.deleted_at = null;
groupOrig.status = 1; friendOrig.status = 1;
} }
groupOrig.persist(); friendOrig.persist();
} }
} }
@ -257,25 +237,5 @@ public class FriendRessources {
@PATCH
@RolesAllowed({"Admin", "User"})
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@Path("enable/{id}")
public Response enableGroup(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) {
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
if (status.equals(Response.Status.OK)) {
GroupEntity groupOrig = GroupEntity.findById(new ObjectId(id));
if (groupOrig == null) {
status = Response.Status.NOT_FOUND;
} else {
groupOrig.status = 1;
groupOrig.persist();
}
}
return Response.status(status).build();
}
} }