Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cron): channel update #482

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions service/cron/docs/cron/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,64 @@ paths:
$ref: '#/components/schemas/CronResponse'
security:
- bearerAuth: []
/api/cron/channels:
get:
tags:
- Channels
summary: Update Channels
description: Update channels
parameters:
- name: platform_type
in: query
required: true
schema:
type: string
enum: [youtube, twitch, twitcasting, niconico]
responses:
"200":
description: Videos updated successfully
content:
application/json:
schema:
type: object
$ref: '#/components/schemas/CronResponse'
"400":
description: Bad Request
content:
application/json:
schema:
type: object
$ref: '#/components/schemas/CronResponse'
"401":
description: Unauthorized
content:
application/json:
schema:
type: object
$ref: '#/components/schemas/CronResponse'
"403":
description: Forbidden
content:
application/json:
schema:
type: object
$ref: '#/components/schemas/CronResponse'
"404":
description: Not Found
content:
application/json:
schema:
type: object
$ref: '#/components/schemas/CronResponse'
"500":
description: Internal Server Error
content:
application/json:
schema:
type: object
$ref: '#/components/schemas/CronResponse'
security:
- bearerAuth: []
/api/ping:
get:
summary: Ping endpoint
Expand Down
14 changes: 9 additions & 5 deletions service/cron/domain/model/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ChannelSnippet struct {
TotalViewCount int
SubscriberCount int
TotalVideoCount int
UpdateAt time.Time
IsDeleted bool
}

Expand All @@ -45,16 +46,19 @@ func (cs Channels) RetrieveYoutubeIDs() []string {
func (cs Channels) FilterUpdateTarget(comparisonChannels Channels) Channels {
return lo.Filter(cs, func(newChannel *Channel, _ int) bool {
// Check if the deleted channel is included in the existing channels
return !lo.SomeBy(comparisonChannels, func(channel *Channel) bool {
return lo.SomeBy(comparisonChannels, func(channel *Channel) bool {
if channel.Youtube.ID == newChannel.Youtube.ID {
if channel.Youtube.Description == newChannel.Youtube.Description {
return false
}
if channel.Youtube.IsDeleted {
return false
}
if channel.Youtube.Name != newChannel.Youtube.Name {
return true
}
if channel.Youtube.ThumbnailURL != newChannel.Youtube.ThumbnailURL {
return true
}
}
return true
return false
})
})
}
13 changes: 13 additions & 0 deletions service/cron/domain/repository/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ type Channel interface {
ctx context.Context,
m model.Channels,
) (model.Channels, error)
Update(
ctx context.Context,
m *model.Channel,
) (*model.Channel, error)
List(
ctx context.Context,
query ListChannelQuery,
) (model.Channels, error)
Exist(
ctx context.Context,
query GetChannelQuery,
Expand All @@ -22,3 +30,8 @@ type GetChannelQuery struct {
ID string
BaseGetOptions
}

type ListChannelQuery struct {
PlatformType string
BaseListOptions
}
41 changes: 39 additions & 2 deletions service/cron/infra/database/internal/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ func CreatorToModel(c *db_sqlc.Creator) *model.Creator {

// ChannelToModel converts db_sqlc.Channel to model.Channel
func ChannelToModel(c *db_sqlc.Channel) *model.Channel {
ch := &model.Channel{}
ch := &model.Channel{
ID: c.ID,
CreatorID: c.CreatorID,
}
snippet := model.ChannelSnippet{
ID: c.ID,
ID: c.PlatformChannelID,
Name: c.Title,
Description: c.Description,
ThumbnailURL: model.ThumbnailURL(c.ThumbnailUrl),
PublishedAt: c.PublishedAt.Time,
TotalViewCount: int(c.TotalViewCount),
SubscriberCount: int(c.SubscriberCount),
TotalVideoCount: int(c.TotalVideoCount),
UpdateAt: c.UpdatedAt.Time,
IsDeleted: c.IsDeleted,
}

Expand All @@ -45,6 +49,15 @@ func ChannelToModel(c *db_sqlc.Channel) *model.Channel {
return ch
}

// ChannelsToModel converts []db_sqlc.Channel to model.Channels
func ChannelsToModel(cs []db_sqlc.Channel) model.Channels {
res := make(model.Channels, 0)
for _, c := range cs {
res = append(res, ChannelToModel(&c))
}
return res
}

// VideoToModel converts db_sqlc.Video to model.Video
func VideoToModel(v *db_sqlc.Video) *model.Video {
m := &model.Video{
Expand Down Expand Up @@ -76,6 +89,7 @@ func CreatorsWithChannelsRowToModel(c *db_sqlc.GetCreatorsWithChannelsRow, creat
TotalViewCount: int(c.Channel.TotalViewCount),
SubscriberCount: int(c.Channel.SubscriberCount),
TotalVideoCount: int(c.Channel.TotalVideoCount),
UpdateAt: c.Channel.UpdatedAt.Time,
IsDeleted: c.Channel.IsDeleted,
}

Expand Down Expand Up @@ -252,6 +266,25 @@ func ChannelModelsToCreateChannelParams(m model.Channels) []db_sqlc.CreateChanne
return ps
}

// ChannelModelToUpdateChannelParams converts model.Channel to db_sqlc.UpdateChannelParams
func ChannelModelToUpdateChannelParams(m *model.Channel) db_sqlc.UpdateChannelParams {
p := db_sqlc.UpdateChannelParams{
Title: m.Youtube.Name,
Description: m.Youtube.Description,
ThumbnailUrl: string(m.Youtube.ThumbnailURL),
UpdatedAt: utime.TimeToTimestamptz(m.Youtube.UpdateAt),
}

if m.Youtube.ID != "" {
p.PlatformChannelID = m.Youtube.ID
} else if m.Twitch.ID != "" {
p.PlatformChannelID = m.Twitch.ID
} else if m.TwitCasting.ID != "" {
p.PlatformChannelID = m.TwitCasting.ID
}
return p
}

// ChannelModelToCreateChannelParams converts model.Channel to db_sqlc.CreateChannelParams
func ChannelModelToCreateChannelParams(m *model.Channel) db_sqlc.CreateChannelParams {
p := db_sqlc.CreateChannelParams{
Expand All @@ -270,6 +303,7 @@ func ChannelModelToCreateChannelParams(m *model.Channel) db_sqlc.CreateChannelPa
p.HiddenSubscriberCount = false
p.TotalVideoCount = int32(m.Youtube.TotalVideoCount)
p.ThumbnailUrl = string(m.Youtube.ThumbnailURL)
p.UpdatedAt = utime.TimeToTimestamptz(m.Youtube.UpdateAt)
p.IsDeleted = m.Youtube.IsDeleted
} else if m.Twitch.ID != "" {
p.ID = m.ID
Expand All @@ -283,6 +317,7 @@ func ChannelModelToCreateChannelParams(m *model.Channel) db_sqlc.CreateChannelPa
p.HiddenSubscriberCount = false
p.TotalVideoCount = int32(m.Twitch.TotalVideoCount)
p.ThumbnailUrl = string(m.Twitch.ThumbnailURL)
p.UpdatedAt = utime.TimeToTimestamptz(m.Twitch.UpdateAt)
p.IsDeleted = m.Twitch.IsDeleted
} else if m.TwitCasting.ID != "" {
p.ID = m.ID
Expand All @@ -296,6 +331,7 @@ func ChannelModelToCreateChannelParams(m *model.Channel) db_sqlc.CreateChannelPa
p.HiddenSubscriberCount = false
p.TotalVideoCount = int32(m.TwitCasting.TotalVideoCount)
p.ThumbnailUrl = string(m.TwitCasting.ThumbnailURL)
p.UpdatedAt = utime.TimeToTimestamptz(m.TwitCasting.UpdateAt)
p.IsDeleted = m.TwitCasting.IsDeleted
} else if m.Niconico.ID != "" {
p.ID = m.ID
Expand All @@ -309,6 +345,7 @@ func ChannelModelToCreateChannelParams(m *model.Channel) db_sqlc.CreateChannelPa
p.HiddenSubscriberCount = false
p.TotalVideoCount = int32(m.Niconico.TotalVideoCount)
p.ThumbnailUrl = string(m.Niconico.ThumbnailURL)
p.UpdatedAt = utime.TimeToTimestamptz(m.Niconico.UpdateAt)
p.IsDeleted = m.Niconico.IsDeleted
}
return p
Expand Down
9 changes: 6 additions & 3 deletions service/cron/infra/database/internal/gen/batch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions service/cron/infra/database/internal/gen/channel.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion service/cron/infra/database/internal/gen/creator.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading