SHARE
TWEET

Untitled

a guest Dec 8th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.hyze.shared.skills.storage;
  2.  
  3. import com.google.common.collect.Lists;
  4. import com.google.common.collect.Maps;
  5. import com.mongodb.client.AggregateIterable;
  6. import com.mongodb.client.MongoCollection;
  7. import com.mongodb.client.MongoDatabase;
  8. import com.mongodb.client.model.Aggregates;
  9. import com.mongodb.client.model.Filters;
  10. import com.mongodb.client.model.Updates;
  11. import net.hyze.core.shared.providers.MongoDatabaseProvider;
  12. import net.hyze.core.shared.storage.repositories.MongoRepository;
  13. import net.hyze.shared.skills.model.SkillLevel;
  14. import net.hyze.shared.skills.user.SkillUser;
  15. import org.bson.Document;
  16. import org.bson.conversions.Bson;
  17.  
  18. import java.util.List;
  19. import java.util.Map;
  20.  
  21. public abstract class AbstractSkillsRepository<T> extends MongoRepository {
  22.  
  23.     public AbstractSkillsRepository(MongoDatabaseProvider databaseProvider) {
  24.         super(databaseProvider);
  25.     }
  26.  
  27.     protected abstract String getIdentifierField();
  28.  
  29.     protected abstract T getIdentifierValue(int userId);
  30.  
  31.     private Map<String, SkillLevel> parse(Document documentParse) {
  32.         Map<String, SkillLevel> mapSkillLevel = Maps.newHashMap();
  33.  
  34.         System.err.println("debug " + documentParse.toString());
  35.  
  36.         documentParse.getList("skills", Document.class).stream()
  37.                 .forEach(document -> {
  38.                     mapSkillLevel.put(document.getString("skill_id"), new SkillLevel(document.getInteger("level"),
  39.                             document.getDouble("xp")));
  40.                 });
  41.  
  42.         return mapSkillLevel;
  43.     }
  44.  
  45.     public Map<String, SkillLevel> fetchById(int userId) {
  46.         MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
  47.         MongoCollection<Document> collection = database.getCollection("users_skills");
  48.  
  49.         AggregateIterable<Document> aggregateIterable = collection.aggregate(Lists.newArrayList(
  50.                 Aggregates.match(Filters.eq(this.getIdentifierField(), this.getIdentifierValue(userId)))
  51.         ));
  52.  
  53.         Document first = aggregateIterable.first();
  54.         if (first != null) {
  55.             System.out.println("debug first");
  56.             return parse(first);
  57.         }
  58.         return null;
  59.     }
  60.  
  61.     public void update(int userId, String skillId, SkillLevel skillLevel) {
  62.         MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
  63.         MongoCollection<Document> collection = database.getCollection("users_skills");
  64.  
  65.         Bson filter = Filters.and(
  66.                 Filters.eq(this.getIdentifierField(), this.getIdentifierValue(userId)),
  67.                 Filters.eq("skills.skill_id", skillId)
  68.         );
  69.  
  70.         collection.updateOne(filter, Updates.set("skills.$.level", skillLevel.getLevel()));
  71.         collection.updateOne(filter, Updates.set("skills.$.xp", skillLevel.getXp()));
  72.     }
  73.  
  74.     public void insert(SkillUser user) {
  75.         MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
  76.         MongoCollection<Document> collection = database.getCollection("users_skills");
  77.  
  78.         Document document = new Document();
  79.         document.put(this.getIdentifierField(), this.getIdentifierValue(user.getId()));
  80.  
  81.         List<Document> documentSkills = Lists.newArrayList();
  82.  
  83.         user.getSkills().forEach((k, v) -> {
  84.             Document documentSkill = new Document();
  85.             documentSkill.put("skill_id", k);
  86.             documentSkill.put("level", v.getLevel());
  87.             documentSkill.put("xp", v.getXp());
  88.             documentSkills.add(documentSkill);
  89.         });
  90.  
  91.         document.put("skills", documentSkills);
  92.  
  93.         collection.insertOne(document);
  94.  
  95.         System.err.println("debug " + document);
  96.     }
  97.  
  98.     public void delete(int userId) {
  99.         MongoDatabase database = this.getDatabaseProvider().provide().getConnection();
  100.         MongoCollection<Document> collection = database.getCollection("users_skills");
  101.         collection.deleteOne(Filters.eq(this.getIdentifierField(), this.getIdentifierValue(userId)));
  102.     }
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top