Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.hyze.shared.skills.storage;
- import com.google.common.collect.Lists;
- import com.google.common.collect.Maps;
- import com.mongodb.client.AggregateIterable;
- import com.mongodb.client.MongoCollection;
- import com.mongodb.client.MongoDatabase;
- import com.mongodb.client.model.Aggregates;
- import com.mongodb.client.model.Filters;
- import com.mongodb.client.model.Updates;
- import net.hyze.core.shared.providers.MongoDatabaseProvider;
- import net.hyze.core.shared.storage.repositories.MongoRepository;
- import net.hyze.shared.skills.model.SkillLevel;
- import net.hyze.shared.skills.user.SkillUser;
- import org.bson.Document;
- import org.bson.conversions.Bson;
- import java.util.List;
- import java.util.Map;
- public abstract class AbstractSkillsRepository<T> extends MongoRepository {
- public AbstractSkillsRepository(MongoDatabaseProvider databaseProvider) {
- super(databaseProvider);
- }
- protected abstract String getIdentifierField();
- protected abstract T getIdentifierValue(int userId);
- private Map<String, SkillLevel> parse(Document documentParse) {
- Map<String, SkillLevel> mapSkillLevel = Maps.newHashMap();
- System.err.println("debug " + documentParse.toString());
- documentParse.getList("skills", Document.class).stream()
- .forEach(document -> {
- mapSkillLevel.put(document.getString("skill_id"), new SkillLevel(document.getInteger("level"),
- document.getDouble("xp")));
- });
- return mapSkillLevel;
- }
- public Map<String, SkillLevel> fetchById(int userId) {
- MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
- MongoCollection<Document> collection = database.getCollection("users_skills");
- AggregateIterable<Document> aggregateIterable = collection.aggregate(Lists.newArrayList(
- Aggregates.match(Filters.eq(this.getIdentifierField(), this.getIdentifierValue(userId)))
- ));
- Document first = aggregateIterable.first();
- if (first != null) {
- System.out.println("debug first");
- return parse(first);
- }
- return null;
- }
- public void update(int userId, String skillId, SkillLevel skillLevel) {
- MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
- MongoCollection<Document> collection = database.getCollection("users_skills");
- Bson filter = Filters.and(
- Filters.eq(this.getIdentifierField(), this.getIdentifierValue(userId)),
- Filters.eq("skills.skill_id", skillId)
- );
- collection.updateOne(filter, Updates.set("skills.$.level", skillLevel.getLevel()));
- collection.updateOne(filter, Updates.set("skills.$.xp", skillLevel.getXp()));
- }
- public void insert(SkillUser user) {
- MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
- MongoCollection<Document> collection = database.getCollection("users_skills");
- Document document = new Document();
- document.put(this.getIdentifierField(), this.getIdentifierValue(user.getId()));
- List<Document> documentSkills = Lists.newArrayList();
- user.getSkills().forEach((k, v) -> {
- Document documentSkill = new Document();
- documentSkill.put("skill_id", k);
- documentSkill.put("level", v.getLevel());
- documentSkill.put("xp", v.getXp());
- documentSkills.add(documentSkill);
- });
- document.put("skills", documentSkills);
- collection.insertOne(document);
- System.err.println("debug " + document);
- }
- public void delete(int userId) {
- MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
- MongoCollection<Document> collection = database.getCollection("users_skills");
- collection.deleteOne(Filters.eq(this.getIdentifierField(), this.getIdentifierValue(userId)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement