Nantes Université

Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 3e0ee75e rédigé par Féry Mathieu (Mathius)'s avatar Féry Mathieu (Mathius)
Parcourir les fichiers

feat(user.posts): Add endpoints for retrieve posts of user

parent b2b014bb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #34325 réussi
......@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import fr.univnantes.webandcloud.api.core.ShortPost;
import fr.univnantes.webandcloud.api.core.ShortUser;
import fr.univnantes.webandcloud.api.core.Token;
import fr.univnantes.webandcloud.api.core.User;
......@@ -26,6 +27,7 @@ import fr.univnantes.webandcloud.api.responses.ResponseError;
import fr.univnantes.webandcloud.api.services.DBService;
import fr.univnantes.webandcloud.api.services.TokenService;
import fr.univnantes.webandcloud.api.services.WithCursor;
import fr.univnantes.webandcloud.api.services.db.DBShortPost;
import fr.univnantes.webandcloud.api.services.db.DBShortUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
......@@ -43,6 +45,8 @@ public class Users extends BaseController {
@Autowired
DBService<Token> tokenDB;
@Autowired
DBShortPost shortPostDB;
@Autowired
TokenService tokenService;
@CrossOrigin
......@@ -71,6 +75,20 @@ public class Users extends BaseController {
return userDB.get(id);
}
@CrossOrigin
@Operation(summary = "Get post of related user")
@ApiResponse(responseCode = "200", description = "Get post of specific user", content = @Content(mediaType = "application/json", schema = @Schema(implementation = WithCursor.class)))
@RequestMapping(value = "/users/{id}/posts", method = RequestMethod.GET)
@Nullable
public WithCursor<List<ShortPost>> getPostOfUser(
@Parameter(description = "Id of user you are looking for", example = "momo54") @PathVariable(value = "id") @NonNull String id,
@Parameter(description = "Limit for request", example = "2") @RequestParam @Nullable Integer limit,
@Parameter(description = "Cursor for next entity", example = "CgA=") @RequestParam @Nullable String cursor)
throws ResponseError {
Cursor cursorObject = cursor != null ? Cursor.fromUrlSafe(cursor) : null;
return shortPostDB.getParentRelatedCreatedDesc(shortUserDB.get(id), limit, cursorObject);
}
@CrossOrigin
@Operation(summary = "Update user")
@ApiResponse(responseCode = "200", description = "Update user", content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)))
......
......@@ -7,16 +7,20 @@ import java.util.List;
import com.google.cloud.datastore.Cursor;
import com.google.cloud.datastore.StructuredQuery.OrderBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.ShortPost;
import fr.univnantes.webandcloud.api.core.ShortUser;
import fr.univnantes.webandcloud.api.services.DBAncestorService;
import fr.univnantes.webandcloud.api.services.WithCursor;
import fr.univnantes.webandcloud.api.services.db.fields.shortPost.AuthorValue;
@Service
public class DBShortPost extends DBAncestorService<ShortPost, ShortUser> {
@Autowired
AuthorValue authorValue;
OrderBy createdDesc = OrderBy.desc("postTime");
public WithCursor<List<ShortPost>> getAllCreatedDesc() {
......@@ -52,4 +56,37 @@ public class DBShortPost extends DBAncestorService<ShortPost, ShortUser> {
ordersList.add(createdDesc);
return getAll(limit, lastCursor, ordersList.toArray(new OrderBy[ordersList.size()]));
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, Integer limit) {
return getParentRelatedCreatedDesc(parent, limit, (Cursor) null);
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, Cursor lastCursor) {
return getParentRelatedCreatedDesc(parent, null, lastCursor);
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, OrderBy... orders) {
return getParentRelatedCreatedDesc(parent, null, null, orders);
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, Integer limit, Cursor lastCursor) {
return getParentRelatedCreatedDesc(parent, limit, lastCursor, new OrderBy[0]);
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, Integer limit, OrderBy... orders) {
return getParentRelatedCreatedDesc(parent, limit, null, orders);
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, Cursor lastCursor,
OrderBy... orders) {
return getParentRelatedCreatedDesc(parent, null, lastCursor, orders);
}
public WithCursor<List<ShortPost>> getParentRelatedCreatedDesc(ShortUser parent, Integer limit, Cursor lastCursor,
OrderBy... orders) {
List<OrderBy> ordersList = new ArrayList<>(Arrays.asList(orders));
ordersList.add(createdDesc);
return searchWith(limit, lastCursor, ordersList.toArray(new OrderBy[ordersList.size()]),
new ShortPost(null, null, null, null, null, parent, null), authorValue);
}
}
package fr.univnantes.webandcloud.api.services.db.fields.shortPost;
import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.ShortPost;
import fr.univnantes.webandcloud.api.core.ShortUser;
import fr.univnantes.webandcloud.api.services.fields.AncestorFieldOf;
@Service
public class AuthorValue extends AncestorFieldOf<ShortPost, ShortUser> {
}
package fr.univnantes.webandcloud.api.services.fields;
import com.google.cloud.datastore.StructuredQuery.Filter;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import fr.univnantes.webandcloud.api.core.WithAncestor;
import fr.univnantes.webandcloud.api.core.WithIdentifier;
import fr.univnantes.webandcloud.api.services.utils.BaseUtils;
public class AncestorFieldOf<EntityUsed extends WithAncestor<Parent>, Parent extends WithIdentifier> extends FilterFieldOf<EntityUsed> {
@Autowired
BaseUtils<Parent> parentsUtils;
@Override
public Filter getFilterRelated(EntityUsed entityUsed) {
return PropertyFilter.hasAncestor(parentsUtils.getKeyOf(entityUsed.getAncestor()));
}
}
......@@ -7,7 +7,9 @@ import com.google.cloud.datastore.Value;
import fr.univnantes.webandcloud.api.core.WithIdentifier;
public abstract class EqualFilterFieldOf<EntityUsed extends WithIdentifier> extends FilterFieldOf<EntityUsed> {
public abstract String getFieldName();
public abstract Value<?> getValue(EntityUsed entityUsed);
public Filter getFilterRelated(EntityUsed entityUsed) {
......
......@@ -14,8 +14,6 @@ public abstract class FilterFieldOf<EntityUsed extends WithIdentifier> {
@Autowired
protected DBService<EntityUsed> DB;
public abstract String getFieldName();
public abstract Filter getFilterRelated(EntityUsed entityUsed);
public WithCursor<List<EntityUsed>> getAllRelated(EntityUsed entityUsed) {
......
......@@ -9,6 +9,8 @@ import fr.univnantes.webandcloud.api.core.WithIdentifier;
public abstract class StartsWithFieldOf<EntityUsed extends WithIdentifier> extends FilterFieldOf<EntityUsed> {
public abstract String getFieldName();
public abstract String getValue(EntityUsed entityUsed);
public Filter getFilterRelated(EntityUsed entityUsed) {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter