Skip to content

Commit

Permalink
add mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
idprm committed Oct 5, 2024
1 parent 6fc8561 commit 76a7ba5
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 65 deletions.
9 changes: 8 additions & 1 deletion cmd/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ func routeUrlListener(db *gorm.DB, rds *redis.Client, rmq rmqp.AMQP, logger *log
teamHandler := handler.NewTeamHandler(teamService)
fixtureHandler := handler.NewFixtureHandler(fixtureService)
predictionHandler := handler.NewPredictionHandler(predictionService)
newsHandler := handler.NewNewsHandler(newsService, followCompetitionService, followTeamService)
newsHandler := handler.NewNewsHandler(
leagueService,
teamService,
newsService,
followCompetitionService,
followTeamService,
&entity.News{},
)

app.Get("/mo", h.MessageOriginated)

Expand Down
11 changes: 11 additions & 0 deletions cmd/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,28 @@ func (p *Processor) News(wg *sync.WaitGroup, message []byte) {
/**
* load repo
*/
leagueRepo := repository.NewLeagueRepository(p.db)
leagueService := services.NewLeagueService(leagueRepo)
teamRepo := repository.NewTeamRepository(p.db)
teamService := services.NewTeamService(teamRepo)
newsRepo := repository.NewNewsRepository(p.db)
newsService := services.NewNewsService(newsRepo)
followCompetitionRepo := repository.NewFollowCompetitionRepository(p.db)
followCompetitionService := services.NewFollowCompetitionService(followCompetitionRepo)
followTeamRepo := repository.NewFollowTeamRepository(p.db)
followTeamService := services.NewFollowTeamService(followTeamRepo)

// parsing json to string
var news *entity.News
json.Unmarshal(message, &news)

h := handler.NewNewsHandler(
leagueService,
teamService,
newsService,
followCompetitionService,
followTeamService,
news,
)

h.Mapping()
Expand Down
5 changes: 5 additions & 0 deletions internal/domain/entity/league.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type League struct {
Logo string `gorm:"size:110" json:"logo"`
Country string `gorm:"size:110" json:"country"`
IsActive bool `gorm:"type:boolean;default:false;column:is_active" json:"is_active"`
Keyword string `json:"keyword"`
}

func (e *League) GetId() int64 {
Expand All @@ -29,3 +30,7 @@ func (e *League) GetLogo() string {
func (e *League) GetCountry() string {
return e.Country
}

func (e *League) GetKeyword() string {
return e.Keyword
}
39 changes: 39 additions & 0 deletions internal/domain/entity/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ func (e *News) GetPublishAt() time.Time {
return e.PublishAt
}

func (e *News) GetParseTitle() string {
if strings.Contains(e.GetTitle(), ":") {
return strings.TrimSpace(e.Title[:strings.IndexByte(e.Title, ':')])
}
return ""
}

func (e *News) GetHomeTeam() string {
return strings.TrimSpace(e.GetParseTitle()[:strings.IndexByte(e.GetParseTitle(), '-')])
}

func (e *News) GetAwayTeam() string {
return strings.TrimSpace(e.GetParseTitle()[strings.IndexByte(e.GetParseTitle(), '-')+1:])
}

func (e *News) IsParseTitle() bool {
return e.GetParseTitle() != ""
}

func (e *News) IsMatch() bool {
return strings.Contains(e.GetParseTitle(), "-")
}

func (e *News) IsMaxiFoot() bool {
return e.Source == "MAXIFOOT"
}

func (e *News) IsMadeInFoot() bool {
return e.Source == "MADEINFOOT"
}

func (e *News) IsAfricaTopSports() bool {
return e.Source == "AFRICATOPSPORTS"
}

func (e *News) IsFootMercato() bool {
return e.Source == "FOOTMERCATO"
}

type NewsSubsciption struct {
ID int64 `gorm:"primaryKey" json:"id"`
SubscriptionID int64 `json:"subscription_id"`
Expand Down
5 changes: 5 additions & 0 deletions internal/domain/entity/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Team struct {
Logo string `json:"logo"`
Founded int `json:"founded"`
Country string `json:"country"`
Keyword string `json:"keyword"`
}

func (e *Team) GetId() int64 {
Expand All @@ -36,3 +37,7 @@ func (e *Team) GetCode() string {
func (e *Team) GetLogo() string {
return e.Logo
}

func (e *Team) GetKeyword() string {
return e.Keyword
}
34 changes: 0 additions & 34 deletions internal/domain/model/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,6 @@ func (s *MORequest) GetMsisdn() string {
return s.Msisdn
}

func (s *MORequest) GetSubKeyword() string {
message := strings.ToUpper(s.SMS)
index := strings.Split(message, " ")

if index[0] == MO_REG || index[0] == MO_UNREG {
if strings.Contains(message, MO_REG) || strings.Contains(message, MO_UNREG) {
if len(index) > 1 {
return index[1]
}
return ""
}
return ""
}
return ""
}

func (s *MORequest) IsREG() bool {
message := strings.ToUpper(s.SMS)
index := strings.Split(message, " ")
if index[0] == MO_REG && (strings.Contains(message, MO_REG)) {
return true
}
return false
}

func (s *MORequest) IsUNREG() bool {
message := strings.ToUpper(s.SMS)
index := strings.Split(message, " ")
if index[0] == MO_UNREG && (strings.Contains(message, MO_UNREG)) {
return true
}
return false
}

func (s *MORequest) GetIpAddress() string {
return s.IpAddress
}
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/repository/league_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *LeagueRepository) CountByPrimaryId(primaryId int) (int64, error) {

func (r *LeagueRepository) CountByName(name string) (int64, error) {
var count int64
err := r.db.Model(&entity.League{}).Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Where("is_active = ?", true).Count(&count).Error
err := r.db.Model(&entity.League{}).Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Or("UPPER(keyword) LIKE UPPER(?)", "%"+name+"%").Where("is_active = ?", true).Count(&count).Error
if err != nil {
return count, err
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (r *LeagueRepository) GetByPrimaryId(primaryId int) (*entity.League, error)

func (r *LeagueRepository) GetByName(name string) (*entity.League, error) {
var c entity.League
err := r.db.Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Where("is_active = ?", true).Take(&c).Error
err := r.db.Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Or("UPPER(keyword) LIKE UPPER(?)", "%"+name+"%").Where("is_active = ?", true).Take(&c).Error
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/repository/team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *TeamRepository) CountByPrimaryId(primaryId int) (int64, error) {

func (r *TeamRepository) CountByName(name string) (int64, error) {
var count int64
err := r.db.Model(&entity.Team{}).Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Count(&count).Error
err := r.db.Model(&entity.Team{}).Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Or("UPPER(keyword) LIKE UPPER(?)", "%"+name+"%").Count(&count).Error
if err != nil {
return count, err
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (r *TeamRepository) GetByPrimaryId(primaryId int) (*entity.Team, error) {

func (r *TeamRepository) GetByName(name string) (*entity.Team, error) {
var c entity.Team
err := r.db.Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Take(&c).Error
err := r.db.Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Or("UPPER(keyword) LIKE UPPER(?)", "%"+name+"%").Take(&c).Error
if err != nil {
return nil, err
}
Expand Down
60 changes: 56 additions & 4 deletions internal/handler/news_handler.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,86 @@
package handler

import (
"fmt"
"log"

"github.com/gofiber/fiber/v2"
"github.com/idprm/go-football-alert/internal/domain/entity"
"github.com/idprm/go-football-alert/internal/domain/model"
"github.com/idprm/go-football-alert/internal/services"
)

type NewsHandler struct {
leagueService services.ILeagueService
teamService services.ITeamService
newsService services.INewsService
followCompetitionService services.IFollowCompetitionService
followTeamService services.IFollowTeamService
news *entity.News
}

func NewNewsHandler(
leagueService services.ILeagueService,
teamService services.ITeamService,
newsService services.INewsService,
followCompetitionService services.IFollowCompetitionService,
followTeamService services.IFollowTeamService,
news *entity.News,
) *NewsHandler {
return &NewsHandler{
leagueService: leagueService,
teamService: teamService,
newsService: newsService,
followCompetitionService: followCompetitionService,
followTeamService: followTeamService,
news: news,
}
}

func (h *NewsHandler) Mapping() {
if h.news.IsParseTitle() {
fmt.Println(h.news.GetParseTitle())
if h.news.IsMatch() {
// home
if h.teamService.IsTeamByName(h.news.GetHomeTeam()) {
team, err := h.teamService.GetByName(h.news.GetHomeTeam())
if err != nil {
log.Println(err.Error())
}
log.Println(team)
}
// away
if h.teamService.IsTeamByName(h.news.GetAwayTeam()) {
team, err := h.teamService.GetByName(h.news.GetAwayTeam())
if err != nil {
log.Println(err.Error())
}
log.Println(team)
}
} else {
// income data by slug
if h.leagueService.IsLeagueByName(h.news.GetParseTitle()) {
league, err := h.leagueService.GetByName(h.news.GetParseTitle())
if err != nil {
log.Println(err.Error())
}
log.Println(league)

}

if h.teamService.IsTeamByName(h.news.GetParseTitle()) {
team, err := h.teamService.GetByName(h.news.GetParseTitle())
if err != nil {
log.Println(err.Error())
}
log.Println(team)
}
}

}

}

func (h *NewsHandler) GetAllPaginate(c *fiber.Ctx) error {
req := new(entity.Pagination)

Expand Down Expand Up @@ -67,7 +123,3 @@ func (h *NewsHandler) Update(c *fiber.Ctx) error {
func (h *NewsHandler) Delete(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "OK"})
}

func (h *NewsHandler) Mapping() {

}
15 changes: 11 additions & 4 deletions internal/handler/scraper_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import (
"github.com/wiliehidayat87/rmqp"
)

const (
SRC_MAXIFOOT string = "MAXIFOOT"
SRC_MADEINFOOT string = "MADEINFOOT"
SRC_AFRICATOPSPORTS string = "AFRICATOPSPORTS"
SRC_FOOTMERCATO string = "FOOTMERCATO"
)

type ScraperHandler struct {
rmq rmqp.AMQP
leagueService services.ILeagueService
Expand Down Expand Up @@ -407,7 +414,7 @@ func (h *ScraperHandler) NewsMaxiFoot() {
Title: el.Title,
Slug: slug.Make(el.Title),
Description: el.Description,
Source: "MAXIFOOT",
Source: SRC_MAXIFOOT,
PublishAt: d,
}
h.newsService.Save(news)
Expand Down Expand Up @@ -445,7 +452,7 @@ func (h *ScraperHandler) NewsMadeInFoot() {
Title: el.Title,
Slug: slug.Make(el.Title),
Description: el.Description,
Source: "MADEINFOOT",
Source: SRC_MADEINFOOT,
PublishAt: d,
}

Expand Down Expand Up @@ -487,7 +494,7 @@ func (h *ScraperHandler) NewsAfricaTopSports() {
Title: el.Title,
Slug: slug.Make(el.Title),
Description: "-",
Source: "AFRICATOPSPORTS",
Source: SRC_AFRICATOPSPORTS,
PublishAt: d,
}

Expand Down Expand Up @@ -524,7 +531,7 @@ func (h *ScraperHandler) NewsFootMercato() {
Title: el.Title,
Slug: slug.Make(el.Title),
Description: el.Keywords,
Source: "FOOTMERCATO",
Source: SRC_FOOTMERCATO,
PublishAt: d,
}

Expand Down
29 changes: 13 additions & 16 deletions internal/handler/sms_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,18 +489,6 @@ func (h *SMSHandler) IsActiveSubByCategory(v string) bool {
return h.subscriptionService.IsActiveSubscriptionByCategory(v, h.req.GetMsisdn())
}

func (h *SMSHandler) getContent(name string) (*entity.Content, error) {
// if data not exist in table contents
if !h.contentService.IsContent(name) {
return &entity.Content{
Category: "CATEGORY",
Channel: "SMS",
Value: "SAMPLE_TEXT",
}, nil
}
return h.contentService.Get(name)
}

func (h *SMSHandler) Firstpush() {
service, err := h.getService()
if err != nil {
Expand Down Expand Up @@ -835,10 +823,19 @@ func (h *SMSHandler) IsSub() bool {
return h.subscriptionService.IsSubscription(service.GetId(), h.req.GetMsisdn())
}

func (h *SMSHandler) IsService() bool {
return h.serviceService.IsService(h.req.GetSubKeyword())
// empty service
func (h *SMSHandler) getService() (*entity.Service, error) {
return h.serviceService.Get("")
}

func (h *SMSHandler) getService() (*entity.Service, error) {
return h.serviceService.Get(h.req.GetSubKeyword())
func (h *SMSHandler) getContent(v string) (*entity.Content, error) {
// if data not exist in table contents
if !h.contentService.IsContent(v) {
return &entity.Content{
Category: "CATEGORY",
Channel: "SMS",
Value: "SAMPLE_TEXT",
}, nil
}
return h.contentService.Get(v)
}
4 changes: 2 additions & 2 deletions internal/services/league_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (s *LeagueService) IsLeagueByPrimaryId(primaryId int) bool {
return count > 0
}

func (s *LeagueService) IsLeagueByName(name string) bool {
count, _ := s.leagueRepo.CountByName(name)
func (s *LeagueService) IsLeagueByName(v string) bool {
count, _ := s.leagueRepo.CountByName(v)
return count > 0
}

Expand Down

0 comments on commit 76a7ba5

Please sign in to comment.