diff --git a/.env b/.env index eecf24f..ecd7c30 100644 --- a/.env +++ b/.env @@ -1,2 +1,6 @@ TELEGRAM_TOKEN= + MONGO_URI= +DATABASE_NAME= +COLLECTION= + diff --git a/broadcasts/telegram.go b/broadcasts/telegram.go index b43ce2e..bba1b5f 100644 --- a/broadcasts/telegram.go +++ b/broadcasts/telegram.go @@ -59,7 +59,7 @@ func (t *Telegram) NewTelegram() { bot.Send(m.Sender, "💡 Nhập tên thành phố có công việc bạn muốn tìm. Ví dụ: /location Hà nội") return } - recruitments, err := t.Repo.FindByLocation(location, "vieclamit") + recruitments, err := t.Repo.FindByLocation(location) if err != nil { fmt.Println(err) } @@ -86,7 +86,7 @@ func (t *Telegram) NewTelegram() { bot.Send(m.Sender, "💡 Nhập tên công ty có công việc bạn muốn tìm. Ví dụ: /company smartosc") return } - recruitments, err := t.Repo.FindByCompany(company, "vieclamit") + recruitments, err := t.Repo.FindByCompany(company) if err != nil { fmt.Println(err) } @@ -113,7 +113,7 @@ func (t *Telegram) NewTelegram() { bot.Send(m.Sender, "💡 Nhập tên kỹ năng bạn muốn tìm. Ví dụ: /skill php") return } - recruitments, err := t.Repo.FindBySkill(skill, "vieclamit") + recruitments, err := t.Repo.FindBySkill(skill) if err != nil { fmt.Println(err) } diff --git a/database/mongo.go b/database/mongo.go index fda6959..d6e0f2b 100644 --- a/database/mongo.go +++ b/database/mongo.go @@ -23,5 +23,5 @@ func (m *Mongo) CreateConn() { if err != nil { log.Fatal(err) } - m.Db = client.Database("") + m.Db = client.Database(os.Getenv("DATABASE_NAME")) } diff --git a/feeds/topcv.go b/feeds/topcv.go index 6714aa1..7b85c97 100644 --- a/feeds/topcv.go +++ b/feeds/topcv.go @@ -23,8 +23,6 @@ import ( const ( topcvBasePath = "https://www.topcv.vn" topcvJobsPath = "/tim-viec-lam-it-phan-mem-c10026" - - collection = "vieclamit" ) // totalPageTopCV get total page @@ -67,7 +65,7 @@ func dataOnePage(url string, repo repository.Repository) error { }) // check url job exists in mongodb - count, err := repo.FindByUrl(urlJob, collection) + count, err := repo.FindByUrl(urlJob) if err != nil { fmt.Println(err) } @@ -135,7 +133,7 @@ func dataOnePage(url string, repo repository.Repository) error { }) // save in to mongodb - errSave := repo.Insert(recruitment, collection) + errSave := repo.Insert(recruitment) if errSave != nil { fmt.Println(errSave) } diff --git a/handle/handle.go b/handle/handle.go index d0d1074..2c59bbe 100644 --- a/handle/handle.go +++ b/handle/handle.go @@ -13,7 +13,7 @@ type Handle struct { // CheckJobDeadlineExpired check expired job deadline func (h *Handle) CheckJobDeadlineExpired() error { - count, err := h.Repo.Delete("vieclamit") + count, err := h.Repo.Delete() if err != nil { return err } diff --git a/repository/repo_impl/repo_impl.go b/repository/repo_impl/repo_impl.go index c1c4d4f..370cadf 100644 --- a/repository/repo_impl/repo_impl.go +++ b/repository/repo_impl/repo_impl.go @@ -3,6 +3,7 @@ package repoimpl import ( "context" "fmt" + "os" "time" "vieclamit/common" @@ -13,6 +14,8 @@ import ( "go.mongodb.org/mongo-driver/bson" ) +var collection = os.Getenv("COLLECTION") + // RepoImpl struct type RepoImpl struct { mg *database.Mongo @@ -26,7 +29,7 @@ func NewRepo(mg *database.Mongo) repository.Repository { } // Insert insert data recruitment in to mongo -func (rp *RepoImpl) Insert(recruitment models.Recruitment, collection string) error { +func (rp *RepoImpl) Insert(recruitment models.Recruitment) error { _, err := rp.mg.Db.Collection(collection).InsertOne(context.TODO(), recruitment) if err != nil { return err @@ -35,7 +38,7 @@ func (rp *RepoImpl) Insert(recruitment models.Recruitment, collection string) er } // FindByUrl find url job to check exists -func (rp *RepoImpl) FindByUrl(urlJob, collection string) (int64, error) { +func (rp *RepoImpl) FindByUrl(urlJob string) (int64, error) { count, err := rp.mg.Db.Collection(collection).CountDocuments(context.TODO(), bson.M{"url_job": urlJob}) if err != nil { return 0, err @@ -44,7 +47,7 @@ func (rp *RepoImpl) FindByUrl(urlJob, collection string) (int64, error) { } // FindByLocation find location -func (rp *RepoImpl) FindByLocation(location, collection string) (*models.Recruitments, error) { +func (rp *RepoImpl) FindByLocation(location string) (*models.Recruitments, error) { var recruitments models.Recruitments cursor, err := rp.mg.Db.Collection(collection).Find(context.TODO(), bson.M{"location": bson.M{"$regex": location, "$options": "i"}}) if err != nil { @@ -57,7 +60,7 @@ func (rp *RepoImpl) FindByLocation(location, collection string) (*models.Recruit } // FindByTitle find skill -func (rp *RepoImpl) FindBySkill(skill, collection string) (*models.Recruitments, error) { +func (rp *RepoImpl) FindBySkill(skill string) (*models.Recruitments, error) { var recruitments models.Recruitments cursor, err := rp.mg.Db.Collection(collection).Find(context.TODO(), bson.M{"title": bson.M{"$regex": skill, "$options": "i"}}) if err != nil { @@ -70,7 +73,7 @@ func (rp *RepoImpl) FindBySkill(skill, collection string) (*models.Recruitments, } // FindByTitle find company -func (rp *RepoImpl) FindByCompany(company, collection string) (*models.Recruitments, error) { +func (rp *RepoImpl) FindByCompany(company string) (*models.Recruitments, error) { var recruitments models.Recruitments cursor, err := rp.mg.Db.Collection(collection).Find(context.TODO(), bson.M{"company": bson.M{"$regex": company, "$options": "i"}}) if err != nil { @@ -83,7 +86,7 @@ func (rp *RepoImpl) FindByCompany(company, collection string) (*models.Recruitme } // FindBySkillAndLocation combine find skill and location -func (rp *RepoImpl) FindBySkillAndLocation(skill, location, collection string) (*models.Recruitments, error) { +func (rp *RepoImpl) FindBySkillAndLocation(skill, location string) (*models.Recruitments, error) { var recruitments models.Recruitments conditions := bson.M{"$and": []bson.M{ {"title": bson.M{"$regex": skill, "$options": "i"}}, @@ -100,7 +103,7 @@ func (rp *RepoImpl) FindBySkillAndLocation(skill, location, collection string) ( } // FindByCompanyAndLocation combine find company and location -func (rp *RepoImpl) FindByCompanyAndLocation(company, location, collection string) (*models.Recruitments, error) { +func (rp *RepoImpl) FindByCompanyAndLocation(company, location string) (*models.Recruitments, error) { var recruitments models.Recruitments conditions := bson.M{"$and": []bson.M{ {"company": bson.M{"$regex": company, "$options": "i"}}, @@ -117,7 +120,7 @@ func (rp *RepoImpl) FindByCompanyAndLocation(company, location, collection strin } // Delete delete document if expired job deadline -func (rp *RepoImpl) Delete(collection string) (int64, error) { +func (rp *RepoImpl) Delete() (int64, error) { timeToday, err := common.ParseTime(time.Now().Format("02/01/2006")) if err != nil { fmt.Println(err) diff --git a/repository/repository.go b/repository/repository.go index 8dff33f..c99a82f 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -4,12 +4,12 @@ import "vieclamit/models" // Repository interface type Repository interface { - Insert(recruitment models.Recruitment, collection string) error - Delete(collection string) (int64, error) - FindByUrl(urlJob, collection string) (int64, error) - FindByLocation(location, collection string) (*models.Recruitments, error) - FindBySkill(skill, collection string) (*models.Recruitments, error) - FindByCompany(company, collection string) (*models.Recruitments, error) - FindBySkillAndLocation(skill, location, collection string) (*models.Recruitments, error) - FindByCompanyAndLocation(company, location, collection string) (*models.Recruitments, error) + Insert(recruitment models.Recruitment) error + Delete() (int64, error) + FindByUrl(urlJob string) (int64, error) + FindByLocation(location string) (*models.Recruitments, error) + FindBySkill(skill string) (*models.Recruitments, error) + FindByCompany(company string) (*models.Recruitments, error) + FindBySkillAndLocation(skill, location string) (*models.Recruitments, error) + FindByCompanyAndLocation(company, location string) (*models.Recruitments, error) }