Skip to content

Commit

Permalink
edit get os env
Browse files Browse the repository at this point in the history
  • Loading branch information
toannd96 committed Mar 1, 2022
1 parent acf3453 commit 62bd6b0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
TELEGRAM_TOKEN=<telegram_token>

MONGO_URI=<mongo_uri>
DATABASE_NAME=<database_name>
COLLECTION=<collection>

6 changes: 3 additions & 3 deletions broadcasts/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
6 changes: 2 additions & 4 deletions feeds/topcv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 11 additions & 8 deletions repository/repo_impl/repo_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repoimpl
import (
"context"
"fmt"
"os"
"time"

"vieclamit/common"
Expand All @@ -13,6 +14,8 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

var collection = os.Getenv("COLLECTION")

// RepoImpl struct
type RepoImpl struct {
mg *database.Mongo
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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"}},
Expand All @@ -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"}},
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 62bd6b0

Please sign in to comment.