mirror of
https://github.com/tiennm99/store-scraper-bot-java.git
synced 2026-05-14 09:53:03 +00:00
Refactor models and add repository layer
Refactored Admin, AppleApp, GoogleApp, and Group models to extend AbstractModel and use Lombok's @Getter/@Setter. Introduced repository classes for Admin, AppleApp, GoogleApp, and Group, each mapping to a specific collection. Added Env enum and updated Config and AbstractRepository to support environment-based scoping.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.miti99.storescraperbot.config;
|
||||
|
||||
import com.miti99.storescraperbot.type.Env;
|
||||
|
||||
public class Config {
|
||||
public static final Env ENV = Env.valueOf(System.getenv("ENV"));
|
||||
|
||||
public static final String COUCHBASE_CONNECTION_STRING =
|
||||
System.getenv("COUCHBASE_CONNECTION_STRING");
|
||||
public static final String COUCHBASE_USERNAME = System.getenv("COUCHBASE_USERNAME");
|
||||
|
||||
@@ -2,8 +2,11 @@ package com.miti99.storescraperbot.model;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Data
|
||||
public class Admin {
|
||||
@Getter
|
||||
@Setter
|
||||
public class Admin extends AbstractModel<String> {
|
||||
List<Long> groups;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.miti99.storescraperbot.model;
|
||||
|
||||
import com.miti99.storescraperbot.api.apple.response.AppleAppResponse;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class AppleApp {
|
||||
@Getter
|
||||
@Setter
|
||||
public class AppleApp extends AbstractModel<String> {
|
||||
long cacheTime;
|
||||
AppleAppResponse rawResponse;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.miti99.storescraperbot.model;
|
||||
|
||||
import com.miti99.storescraperbot.api.google.response.GoogleAppResponse;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class GoogleApp {
|
||||
@Getter
|
||||
@Setter
|
||||
public class GoogleApp extends AbstractModel<String> {
|
||||
long cacheTime;
|
||||
GoogleAppResponse rawResponse;
|
||||
}
|
||||
|
||||
@@ -3,9 +3,12 @@ package com.miti99.storescraperbot.model;
|
||||
import com.miti99.storescraperbot.type.AppType;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Data
|
||||
public class Group {
|
||||
@Getter
|
||||
@Setter
|
||||
public class Group extends AbstractModel<Long> {
|
||||
List<App> apps;
|
||||
|
||||
public static class App {
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
package com.miti99.storescraperbot.repository;
|
||||
|
||||
import com.couchbase.client.java.Collection;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.miti99.storescraperbot.config.Config;
|
||||
import com.miti99.storescraperbot.model.AbstractModel;
|
||||
import com.miti99.storescraperbot.util.CouchbaseUtil;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
/** 1 repository = 1 collection */
|
||||
@Log4j2
|
||||
public abstract class AbstractRepository<K, V extends AbstractModel<K>> {
|
||||
public static final String SEPARATOR = "_";
|
||||
// protected static ObjectMapper objectMapper = new ObjectMapper();
|
||||
protected final Class<V> classV = getDataClass();
|
||||
// protected final JavaType type = objectMapper.getTypeFactory().constructType(classV);
|
||||
protected final String scopeName;
|
||||
protected final String scopeName = Config.ENV.name().toLowerCase();
|
||||
protected final String collectionName;
|
||||
|
||||
protected AbstractRepository(String scopeName, String collectionName) {
|
||||
this.scopeName = scopeName;
|
||||
this.collectionName = collectionName;
|
||||
protected AbstractRepository(String collectionName) {
|
||||
this.collectionName = collectionName.toLowerCase();
|
||||
}
|
||||
|
||||
public Collection collection() {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.miti99.storescraperbot.repository;
|
||||
|
||||
import com.miti99.storescraperbot.model.Admin;
|
||||
|
||||
/**
|
||||
* Đây là repository chỉ chứa 1 key duy nhất, key là "" (rỗng)
|
||||
*/
|
||||
public class AdminRepository extends AbstractRepository<String, Admin> {
|
||||
private static final AdminRepository INSTANCE = new AdminRepository();
|
||||
|
||||
protected AdminRepository() {
|
||||
super("admin");
|
||||
}
|
||||
|
||||
public void init() {
|
||||
init("");
|
||||
}
|
||||
|
||||
public Admin load() {
|
||||
return load("");
|
||||
}
|
||||
|
||||
public void save(Admin data) {
|
||||
save("", data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.miti99.storescraperbot.repository;
|
||||
|
||||
import com.miti99.storescraperbot.model.AppleApp;
|
||||
import com.miti99.storescraperbot.model.Group;
|
||||
|
||||
public class AppleAppRepository extends AbstractRepository<String, AppleApp> {
|
||||
public static final AppleAppRepository INSTANCE = new AppleAppRepository();
|
||||
|
||||
protected AppleAppRepository() {
|
||||
super("apple");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.miti99.storescraperbot.repository;
|
||||
|
||||
import com.miti99.storescraperbot.model.GoogleApp;
|
||||
import com.miti99.storescraperbot.model.Group;
|
||||
|
||||
public class GoogleAppRepository extends AbstractRepository<String, GoogleApp> {
|
||||
private static final GoogleAppRepository INSTANCE = new GoogleAppRepository();
|
||||
|
||||
protected GoogleAppRepository() {
|
||||
super("google");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.miti99.storescraperbot.repository;
|
||||
|
||||
import com.miti99.storescraperbot.model.Group;
|
||||
|
||||
public class GroupRepository extends AbstractRepository<Long, Group> {
|
||||
public static final GroupRepository INSTANCE = new GroupRepository();
|
||||
|
||||
protected GroupRepository() {
|
||||
super("group");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.miti99.storescraperbot.type;
|
||||
|
||||
public enum Env {
|
||||
DEVELOPMENT,
|
||||
PRODUCTION
|
||||
}
|
||||
Reference in New Issue
Block a user