-
Notifications
You must be signed in to change notification settings - Fork 0
Model Services
Having to make requests for each of the existing models or models that will be made in the future can become a bit annoying and will compromise the scalability of Centauri.
Thinking in avoiding repeating code simple CRUD operations of different models we have created a service system that is complemented with the Model Hierarchy.
To meet the ModelRequest requirements, the following routes must be available in the backend pointed to by the HttpClientConfig:
| Request Type | Route | HTTP Method |
|---|---|---|
| CreateService | /model/ |
POST |
| FindService | /model/:id |
GET |
| PaginateService | /model/list |
POST |
| QueryService | /model/list |
POST |
| UpdateService | /model/ |
PUT |
| Delete | /model/:id |
DELETE |
Query parameters like page and perPage can be provided at PaginateService and will return a different object called Pagination.
Do you remember the Friendship model we created earlier?: If not, you can take a look at the Model Hierarchy section.
Our Frienship model has an interface called Friendship.Partial which we previously linked to a base Relation interface and a called Creation that contains all the necessary data to create the friendship, including whether it was forced or not.
Now, let's take a look at how service injection would work:
private @Inject CreateService<Friendship, FriendshipDoc.Partial> createService;Looks good, doesn't it? In the first generic of the service we are going to put the complete class (The one that extends the Model) and the Partial class, the one that from now on will be in charge of creating the friendship.
The next thing we will call the create() method and pass it an instance of the Partial. It is up to you if you do it with a specific class or create a temporary implementation of any of the creation interfaces.
For this example we will use a temporal implementation:
Friendship friend = friendshipService.createSync(new FriendshipDoc.Relation() {
@Override
public String getSender() {
return "senderId";
}
@Override
public String getReceiver() {
return "receiverId";
}
});Voila! We have created a new friendship in the backend database. However, remember that there is a lot of process behind this that you must do, such as detecting if they are already friends, if the user has requests disabled, etc ... All the business logic is in your hands.
At some point in the implementation of a new function in Centauri you will need the ID of some object. In this example we will use a User.
private @Inject FindService<User, UserDoc.Partial> findService;This function is really very easy, you just have to send the ID of the database and it will return the complete model.
Note: Our custom version of Spigot has a built-in function in the Player object to get its ID directly, so we will use this in the example.
User user = findService.findSync(player.getDatabaseIdentifier());Now, since we have a User object updating it is not particularly difficult. As long as you have defined a setter for a certain attribute of a Model, you will simply have to update its value and provide it through the service. Let's start by injecting the user service:
private @Inject UpdateService<User, UserDoc.Partial> updateService;Now, we are going to use the model that we obtained previously and update its skin:
User user = findService.findSync(player.getDatabaseIdentifier());
user.setSkin("MomlessTomato");Now, all you need to do now is to use the update service and you are done.
There is a way to search for certain models that meet one or more conditions. For this you will have to create a Mongo query with Jackson's JsonNode and send it to the service. Let's do some carpentry:
ObjectNode sender = this.objectMapper.createObjectNode();
sender.put("sender", "senderId");
sender.put("receiver", "receiverId");
ObjectNode receiver = this.objectMapper.createObjectNode();
receiver.put("sender", "senderId");
receiver.put("receiver", "receiverId");
List<ObjectNode> nodes = new ArrayList<>();
nodes.add(sender);
nodes.add(receiver);
ObjectNode finalRequest = this.objectMapper.createObjectNode();
finalRequest.putArray("$or").addAll(nodes);It is not fancy code but it clearly shows what it will print as Query: {"$ or": [{"sender": "senderId", "receiver": "receiverId"}, {"sender": "senderId", "receiver": "receiverId"}]}. All you have to do is replace the test values with actual values and send it to the service.
Collection<Friendship> friendships = friendshipFindService.querySync(finalRequest).getFoundModels();NOTE: You will get a empty collection in case that your query does not match any database record.
The PaginationService is very similar to the QueryService. In fact you can send the same query as in it, but with the difference that you can send some parameters that will give you a Pagination object.
All you have to supply are some of the following options in a Query String.
Here is a list of options that you can use to send your request:
| Value | Description |
|---|---|
sort |
Will return items ordened by a certain property |
page |
Will return a certain page over a pagination |
perPage |
Will return the page size related to the page
|
Now, to use the PaginateService you just have to call it exactly the same as the QueryService, adding the Query String.
PaginateResult.Pagination serverPagination = paginateRequest.paginateSync("?page=1&perPage=15", finalRequest);NOTE: Be The Cooler Daniel of programming and create your own way to generate automatically the Query String.
The saddest part of any model is being eliminated, we know that. However, that is something you should not be concerned about. The only thing you need to delete is the record ID.
this.deleteService.delete(id);For now, this is all you need to know regarding Model Services. However, you can take advantage of its structure and add your own requests. Try it!
- Understanding Session Registry
- Configuring a server
- Authentication Gateways
- Invalid Auth & Cooldowns
- Introduction
- Matchmaking
- Server Pairing
- Match Scheduling
- Lobby Functionality
- Stats
- Match Event Handling