-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
696 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ cmd/service | |
internal/generated | ||
|
||
internal/adapters/discord | ||
internal/adapters/omdb | ||
|
||
internal/db | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package omdb | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/nijeti/cinema-keeper/internal/models" | ||
"github.com/nijeti/cinema-keeper/internal/pkg/ptr" | ||
) | ||
|
||
type Config struct { | ||
Key string `conf:"key"` | ||
} | ||
|
||
type Adapter struct { | ||
config Config | ||
client *http.Client | ||
} | ||
|
||
func New(config Config) *Adapter { | ||
return &Adapter{ | ||
config: config, | ||
client: &http.Client{}, | ||
} | ||
} | ||
|
||
func (a *Adapter) MoviesByTitle( | ||
ctx context.Context, title string, | ||
) ([]models.MovieShort, error) { | ||
var dto movieSearch | ||
if err := a.makeRequest(ctx, a.baseURL()+"&s="+title, &dto); err != nil { | ||
return nil, err | ||
} | ||
|
||
return dto.toModel(), nil | ||
} | ||
|
||
func (a *Adapter) MovieByID( | ||
ctx context.Context, id string, | ||
) (*models.MovieShort, error) { | ||
var dto movie | ||
if err := a.makeRequest(ctx, a.baseURL()+"&i="+id, &dto); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ptr.To(dto.toModel()), nil | ||
} | ||
|
||
func (a *Adapter) makeRequest( | ||
ctx context.Context, url string, result any, | ||
) error { | ||
req, err := http.NewRequestWithContext( | ||
ctx, http.MethodGet, url, http.NoBody, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
resp, err := a.client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to get response: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
err = json.NewDecoder(resp.Body).Decode(result) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal response body: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package omdb | ||
|
||
import ( | ||
"github.com/nijeti/cinema-keeper/internal/models" | ||
) | ||
|
||
//nolint:tagliatelle // external model | ||
type movie struct { | ||
IMDBID string `json:"imdbID"` | ||
Title string `json:"Title"` | ||
} | ||
|
||
type movieSearch struct { | ||
Search []movie | ||
} | ||
|
||
func (m movie) toModel() models.MovieShort { | ||
return models.MovieShort{ | ||
ID: models.IMDBID(m.IMDBID), | ||
Title: m.Title, | ||
} | ||
} | ||
|
||
func (s movieSearch) toModel() []models.MovieShort { | ||
movies := make([]models.MovieShort, 0, len(s.Search)) | ||
for _, m := range s.Search { | ||
movies = append(movies, m.toModel()) | ||
} | ||
return movies | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package omdb | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ( | ||
host = "http://www.omdbapi.com" | ||
apiKeyParam = "apikey" | ||
) | ||
|
||
func (a *Adapter) baseURL() string { | ||
return fmt.Sprintf("%s/?%s=%s", host, apiKeyParam, a.config.Key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package responses | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
|
||
"github.com/nijeti/cinema-keeper/internal/models" | ||
) | ||
|
||
func MovieAutocompleteEmptySearch() *discordgo.InteractionResponse { | ||
return &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionApplicationCommandAutocompleteResult, | ||
Data: &discordgo.InteractionResponseData{ | ||
Choices: []*discordgo.ApplicationCommandOptionChoice{}, | ||
}, | ||
} | ||
} | ||
|
||
func MovieAutocompleteSearch( | ||
movies []models.MovieShort, | ||
) *discordgo.InteractionResponse { | ||
choices := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(movies)) | ||
for _, movie := range movies { | ||
choices = append( | ||
choices, &discordgo.ApplicationCommandOptionChoice{ | ||
Name: movie.Title, | ||
Value: movie.ID, | ||
}, | ||
) | ||
} | ||
|
||
return &discordgo.InteractionResponse{ | ||
Type: discordgo.InteractionApplicationCommandAutocompleteResult, | ||
Data: &discordgo.InteractionResponseData{ | ||
Choices: choices, | ||
}, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
internal/generated/mocks/services/searchNewMovie/discord.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.