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 Collection<String> friends;
public Short status;
public LocalDateTime created_at;
public LocalDateTime updated_at;

View File

@ -155,22 +155,22 @@ public class FriendRessources {
@RolesAllowed({"Admin", "User"})
@Path("{id}")
@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));
UsersEntity user = UsersEntity.findById(kid);
Response.Status status = getResponseCheck(ctx, userCookie, user);
GroupEntity newGroup = new GroupEntity();
FriendEntity newFriend = new FriendEntity();
if (status.equals(Response.Status.OK)) {
newGroup.name = group.name;
newGroup.admins = group.admins;
newGroup.created_at = LocalDateTime.now();
newGroup.updated_at = LocalDateTime.now();
newGroup.persist();
newFriend.owner = friend.owner;
newFriend.friends = friend.friends;
newFriend.created_at = LocalDateTime.now();
newFriend.updated_at = LocalDateTime.now();
newFriend.persist();
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);
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) {
FriendEntity singleFriend = FriendEntity.find("id", new ObjectId(id)).firstResult();
if (singleFriend == null) {
status = Response.Status.NOT_FOUND;
} else {
singleGroup.status = -1;
singleGroup.updated_at = LocalDateTime.now();
singleGroup.deleted_at = LocalDateTime.now();
singleGroup.persist();
singleFriend.status = -1;
singleFriend.updated_at = LocalDateTime.now();
singleFriend.deleted_at = LocalDateTime.now();
singleFriend.persist();
}
}
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
@ -228,27 +207,28 @@ public class FriendRessources {
@Transactional
@Path("{id}")
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));
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) {
FriendEntity friendOrig = FriendEntity.findById(new ObjectId(id));
if (friendOrig == null) {
status = Response.Status.NOT_FOUND;
} 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){
groupOrig.deleted_at = null;
groupOrig.status = 1;
if(friend.status == 1){
friendOrig.deleted_at = null;
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();
}
}