Getting last 10 product view by user id

50 Views Asked by At

How can i getting last 10 product views by user(with userid) in json format.

My Event Class:

@Getter
@Setter
@Document(collection = "events")
public class Event {

    @Id
    private String id;
    private String event;
    private String messageid;
    private String userid;
    private ProductProperties properties;
    private Context context;
    private String timestamp;

}

My ProductProperties Class:

@Getter
@Setter
public class ProductProperties {

    private String productid;

}

My Context Class:

@Getter
@Setter
public class Context {

    private String source;

}

My Repository:

@Repository
public interface EventRepository extends MongoRepository<Event, String> {
    List<Event> findByUseridAndEventOrderByTimestampDesc(String userId, String event, Pageable pageable);

}

My Service Method:

public List<Event> getLast10ProductViewsByUserId(String userId) {
        return eventRepository.findByUseridAndEventOrderByTimestampDesc(userId, "ProductView", PageRequest.of(0, 10));
    }

My Database tables example:

enter image description here

I want result with a get request like this:


{
    "user-id": "user-34",
    "products": ["a","b","c","d"."e"],
    "type" : "personalized"
}

I tried pagable but i guess i could not make it.

0

There are 0 best solutions below