Skip to content

Commit

Permalink
Rework models (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiJeTi authored Jan 26, 2025
1 parent 6944301 commit 41750fc
Show file tree
Hide file tree
Showing 50 changed files with 661 additions and 391 deletions.
7 changes: 4 additions & 3 deletions cmd/migrator/00003_movies.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
create table "movies"
(
"id" serial primary key,
"imdb_id" varchar(9) not null unique,
"imdb_id" varchar(16) not null unique,
"title" varchar(100) not null,
"year" integer not null,
"year" varchar(10) not null,
"genre" varchar(50) not null,
"director" varchar(100) not null,
"plot" text not null,
"poster_url" varchar(256) not null
Expand All @@ -19,7 +20,7 @@ create table "guild_movies"
"added_by_id" varchar(20) not null,
"added_at" timestamptz not null,
"watched_at" timestamptz,
"rating" smallint check ("rating" in (-1, 1)),
"rating" int2 check ("rating" in (-1, 1)),

constraint "fk_movies" foreign key ("movie_id")
references "movies" ("id") on delete cascade
Expand Down
10 changes: 5 additions & 5 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
hcHTTP "github.com/nijeti/healthcheck/servers/http"

dcAdapterPkg "github.com/nijeti/cinema-keeper/internal/adapters/discord"
"github.com/nijeti/cinema-keeper/internal/adapters/omdb"
"github.com/nijeti/cinema-keeper/internal/db"
"github.com/nijeti/cinema-keeper/internal/discord"
"github.com/nijeti/cinema-keeper/internal/discord/commands"
Expand All @@ -34,6 +35,7 @@ import (
type config struct {
Discord discord.Config `conf:"discord"`
DB db.Config `conf:"db"`
OMDB omdb.Config `conf:"omdb"`
}

const (
Expand Down Expand Up @@ -70,8 +72,6 @@ func run() (code int) {
}

// db
dbLogger := logger.WithGroup("db")

dbConn, err := db.Connect(ctx, cfg.DB)
if err != nil {
logger.ErrorContext(ctx, "failed to connect to db", "error", err)
Expand All @@ -81,7 +81,7 @@ func run() (code int) {

dbProbe := db.NewProbe(dbConn.DB)

quotesRepo := db.NewQuotesRepo(dbLogger, dbConn)
quotesRepo := db.NewQuotesRepo(dbConn)

// discord
dcLogger := logger.WithGroup("discord")
Expand All @@ -102,12 +102,12 @@ func run() (code int) {
dcAdapter := dcAdapterPkg.New(dcRouter.Session())

addQuoteSvc := addQuote.New(dcAdapter, quotesRepo)
diceRollSvc := diceRoll.New(dcAdapter)
listUserQuotesSvc := listUserQuotes.New(dcAdapter, quotesRepo)
lockVoiceChanSvc := lockVoiceChan.New(dcAdapter)
mentionVoiceChanSvc := mentionVoiceChan.New(dcAdapter)
presenceSvc := presence.New(dcAdapter)
printRandomQuoteSvc := printRandomQuote.New(quotesRepo, dcAdapter)
rollSvc := diceRoll.New(dcAdapter)
unlockVoiceChanSvc := unlockVoiceChan.New(dcAdapter)

err = dcRouter.SetCommands(
Expand All @@ -131,7 +131,7 @@ func run() (code int) {
},
discord.Command{
Description: commands.Roll(),
Handler: roll.New(rollSvc),
Handler: roll.New(diceRollSvc),
},
discord.Command{
Description: commands.Movie(),
Expand Down
33 changes: 17 additions & 16 deletions internal/adapters/discord/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func (a *Adapter) Respond(
}

func (a *Adapter) GuildMember(
ctx context.Context, guildID models.ID, userID models.ID,
ctx context.Context, guildID models.DiscordID, userID models.DiscordID,
) (*discordgo.Member, error) {
member, err := a.session.GuildMember(
guildID.String(), userID.String(), discordgo.WithContext(ctx),
string(guildID), string(userID), discordgo.WithContext(ctx),
)
if err != nil {
return nil, fmt.Errorf("failed to get guild member: %w", err)
Expand All @@ -54,10 +54,10 @@ func (a *Adapter) GuildMember(
}

func (a *Adapter) Channel(
ctx context.Context, channelID models.ID,
ctx context.Context, channelID models.DiscordID,
) (*discordgo.Channel, error) {
channel, err := a.session.Channel(
channelID.String(), discordgo.WithContext(ctx),
string(channelID), discordgo.WithContext(ctx),
)
if err != nil {
return nil, fmt.Errorf("failed to get channel: %w", err)
Expand All @@ -67,32 +67,32 @@ func (a *Adapter) Channel(
}

func (a *Adapter) UserVoiceState(
_ context.Context, guildID models.ID, userID models.ID,
_ context.Context, guildID models.DiscordID, userID models.DiscordID,
) (*discordgo.VoiceState, error) {
voiceState, err := a.session.State.VoiceState(
guildID.String(), userID.String(),
string(guildID), string(userID),
)

switch {
case err == nil:
return voiceState, nil
case errors.Is(err, discordgo.ErrStateNotFound):
return nil, nil //nolint:nilnil // nil channel ID is a valid result
return nil, nil //nolint:nilnil // nil channel DiscordID is a valid result
default:
return nil, fmt.Errorf("failed to get user voice state: %w", err)
}
}

func (a *Adapter) VoiceChannelUsers(
ctx context.Context, guildID models.ID, channelID models.ID,
ctx context.Context, guildID models.DiscordID, channelID models.DiscordID,
) ([]*discordgo.Member, error) {
var users []*discordgo.Member

var after string
for {
const maxMembers = 1000
members, err := a.session.GuildMembers(
guildID.String(), after, maxMembers, discordgo.WithContext(ctx),
string(guildID), after, maxMembers, discordgo.WithContext(ctx),
)
if err != nil {
return nil, fmt.Errorf("failed to get guild members: %w", err)
Expand All @@ -104,7 +104,7 @@ func (a *Adapter) VoiceChannelUsers(

for _, member := range members {
voiceState, err := a.session.State.VoiceState(
guildID.String(), member.User.ID,
string(guildID), member.User.ID,
)
if err != nil {
if errors.Is(err, discordgo.ErrStateNotFound) {
Expand All @@ -113,7 +113,7 @@ func (a *Adapter) VoiceChannelUsers(
return nil, fmt.Errorf("failed to get voice state: %w", err)
}

if voiceState.ChannelID != channelID.String() {
if voiceState.ChannelID != string(channelID) {
continue
}

Expand All @@ -127,10 +127,11 @@ func (a *Adapter) VoiceChannelUsers(
}

func (a *Adapter) EditChannel(
ctx context.Context, channelID models.ID, edit *discordgo.ChannelEdit,
ctx context.Context, channelID models.DiscordID,
edit *discordgo.ChannelEdit,
) error {
_, err := a.session.ChannelEdit(
channelID.String(), edit, discordgo.WithContext(ctx),
string(channelID), edit, discordgo.WithContext(ctx),
)
if err != nil {
return fmt.Errorf("failed to edit channel: %w", err)
Expand All @@ -143,17 +144,17 @@ func (a *Adapter) EditChannel(
// Use this method to remove user limit.
// Package "discordgo" has a flaw which disallows to do it with EditChannel.
func (a *Adapter) ChannelUnsetUserLimit(
ctx context.Context, channelID models.ID,
ctx context.Context, channelID models.DiscordID,
) error {
type request struct {
UserLimit int `json:"user_limit"`
}

_, err := a.session.RequestWithBucketID(
http.MethodPatch,
discordgo.EndpointChannel(channelID.String()),
discordgo.EndpointChannel(string(channelID)),
request{UserLimit: 0},
discordgo.EndpointChannel(channelID.String()),
discordgo.EndpointChannel(string(channelID)),
discordgo.WithContext(ctx),
)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions internal/adapters/omdb/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ func New(config Config) *Adapter {
func (a *Adapter) MoviesByTitle(
ctx context.Context, title string,
) ([]models.MovieBase, error) {
url := a.baseURL() + "&s=" + title

var dto search
if err := a.makeRequest(ctx, a.baseURL()+"&s="+title, &dto); err != nil {
if err := a.makeRequest(ctx, url, &dto); err != nil {
return nil, err
}

return dto.toModel(), nil
}

func (a *Adapter) MovieByID(
ctx context.Context, id string,
ctx context.Context, id models.ImdbID,
) (*models.MovieMeta, error) {
url := a.baseURL() + "&i=" + string(id)

var dto movie
if err := a.makeRequest(ctx, a.baseURL()+"&i="+id, &dto); err != nil {
if err := a.makeRequest(ctx, url, &dto); err != nil {
return nil, err
}

Expand Down
16 changes: 5 additions & 11 deletions internal/adapters/omdb/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
package omdb

import (
"fmt"
"strconv"

"github.com/nijeti/cinema-keeper/internal/models"
)

type movieBase struct {
IMDBID string `json:"imdbID"`
ImdbID string `json:"imdbID"`
Title string `json:"Title"`
Year string `json:"Year"`
}

type movie struct {
movieBase
Genre string `json:"Genre"`
Director string `json:"Director"`
Plot string `json:"Plot"`
PosterURL string `json:"Poster"`
Expand All @@ -26,21 +24,17 @@ type search struct {
}

func (m movieBase) toModel() models.MovieBase {
year, err := strconv.Atoi(m.Year)
if err != nil {
panic(fmt.Errorf("failed to parse year: %w", err))
}

return models.MovieBase{
ID: models.IMDBID(m.IMDBID),
ID: models.ImdbID(m.ImdbID),
Title: m.Title,
Year: year,
Year: m.Year,
}
}

func (m *movie) toModel() *models.MovieMeta {
return &models.MovieMeta{
MovieBase: m.movieBase.toModel(),
Genre: m.Genre,
Director: m.Director,
Plot: m.Plot,
PosterURL: m.PosterURL,
Expand Down
Loading

0 comments on commit 41750fc

Please sign in to comment.