- Implemented Create, Read, Update (PUT/PATCH), and Delete (CRUD) operations for films. - Added SQL queries for film management (e.g., NextFilmID, InsertFilm, UpdateFilmPut, PatchFilm, and DeleteFilm). - Updated HTTP handlers to support enhanced film operations, including validation and null handling. - Refactored category handling logic for cleaner syntax. - Included HTTP client test samples for film API in `films_crud_test.http`.
340 lines
8.2 KiB
Go
340 lines
8.2 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: film.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
const countFilms = `-- name: CountFilms :one
|
|
SELECT COUNT(*)
|
|
FROM film
|
|
`
|
|
|
|
func (q *Queries) CountFilms(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRowContext(ctx, countFilms)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const getFilm = `-- name: GetFilm :one
|
|
SELECT film_id,
|
|
title,
|
|
description,
|
|
release_year,
|
|
language_id,
|
|
original_language_id,
|
|
rental_duration,
|
|
rental_rate,
|
|
length,
|
|
replacement_cost,
|
|
rating,
|
|
special_features,
|
|
last_update
|
|
FROM film
|
|
WHERE film_id = ?
|
|
`
|
|
|
|
func (q *Queries) GetFilm(ctx context.Context, filmID int64) (Film, error) {
|
|
row := q.db.QueryRowContext(ctx, getFilm, filmID)
|
|
var i Film
|
|
err := row.Scan(
|
|
&i.FilmID,
|
|
&i.Title,
|
|
&i.Description,
|
|
&i.ReleaseYear,
|
|
&i.LanguageID,
|
|
&i.OriginalLanguageID,
|
|
&i.RentalDuration,
|
|
&i.RentalRate,
|
|
&i.Length,
|
|
&i.ReplacementCost,
|
|
&i.Rating,
|
|
&i.SpecialFeatures,
|
|
&i.LastUpdate,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listActorsByFilm = `-- name: ListActorsByFilm :many
|
|
SELECT a.actor_id, a.first_name, a.last_name, a.last_update
|
|
FROM actor a
|
|
JOIN film_actor fa ON fa.actor_id = a.actor_id
|
|
WHERE fa.film_id = ?
|
|
ORDER BY a.last_name ASC, a.first_name ASC
|
|
`
|
|
|
|
func (q *Queries) ListActorsByFilm(ctx context.Context, filmID int64) ([]Actor, error) {
|
|
rows, err := q.db.QueryContext(ctx, listActorsByFilm, filmID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Actor
|
|
for rows.Next() {
|
|
var i Actor
|
|
if err := rows.Scan(
|
|
&i.ActorID,
|
|
&i.FirstName,
|
|
&i.LastName,
|
|
&i.LastUpdate,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listFilms = `-- name: ListFilms :many
|
|
SELECT film_id,
|
|
title,
|
|
description,
|
|
release_year,
|
|
language_id,
|
|
original_language_id,
|
|
rental_duration,
|
|
rental_rate,
|
|
length,
|
|
replacement_cost,
|
|
rating,
|
|
special_features,
|
|
last_update
|
|
FROM film
|
|
ORDER BY film_id ASC
|
|
LIMIT ? OFFSET ?
|
|
`
|
|
|
|
type ListFilmsParams struct {
|
|
Limit int64 `json:"limit"`
|
|
Offset int64 `json:"offset"`
|
|
}
|
|
|
|
func (q *Queries) ListFilms(ctx context.Context, arg ListFilmsParams) ([]Film, error) {
|
|
rows, err := q.db.QueryContext(ctx, listFilms, arg.Limit, arg.Offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Film
|
|
for rows.Next() {
|
|
var i Film
|
|
if err := rows.Scan(
|
|
&i.FilmID,
|
|
&i.Title,
|
|
&i.Description,
|
|
&i.ReleaseYear,
|
|
&i.LanguageID,
|
|
&i.OriginalLanguageID,
|
|
&i.RentalDuration,
|
|
&i.RentalRate,
|
|
&i.Length,
|
|
&i.ReplacementCost,
|
|
&i.Rating,
|
|
&i.SpecialFeatures,
|
|
&i.LastUpdate,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// Additional film CRUD methods (manually added to mirror sqlc patterns)
|
|
const nextFilmID = `-- name: NextFilmID :one
|
|
SELECT COALESCE(MAX(film_id), 0) + 1 FROM film
|
|
`
|
|
|
|
func (q *Queries) NextFilmID(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRowContext(ctx, nextFilmID)
|
|
var id int64
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const insertFilm = `-- name: InsertFilm :exec
|
|
INSERT INTO film (
|
|
film_id,
|
|
title,
|
|
description,
|
|
release_year,
|
|
language_id,
|
|
original_language_id,
|
|
rental_duration,
|
|
rental_rate,
|
|
length,
|
|
replacement_cost,
|
|
rating,
|
|
special_features,
|
|
last_update
|
|
) VALUES (
|
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP
|
|
)
|
|
`
|
|
|
|
type InsertFilmParams struct {
|
|
FilmID int64 `json:"film_id"`
|
|
Title string `json:"title"`
|
|
Description interface{} `json:"description"`
|
|
ReleaseYear sql.NullString `json:"release_year"`
|
|
LanguageID int64 `json:"language_id"`
|
|
OriginalLanguageID sql.NullInt64 `json:"original_language_id"`
|
|
RentalDuration int64 `json:"rental_duration"`
|
|
RentalRate float64 `json:"rental_rate"`
|
|
Length sql.NullInt64 `json:"length"`
|
|
ReplacementCost float64 `json:"replacement_cost"`
|
|
Rating sql.NullString `json:"rating"`
|
|
SpecialFeatures sql.NullString `json:"special_features"`
|
|
}
|
|
|
|
func (q *Queries) InsertFilm(ctx context.Context, arg InsertFilmParams) error {
|
|
_, err := q.db.ExecContext(ctx, insertFilm,
|
|
arg.FilmID,
|
|
arg.Title,
|
|
arg.Description,
|
|
arg.ReleaseYear,
|
|
arg.LanguageID,
|
|
arg.OriginalLanguageID,
|
|
arg.RentalDuration,
|
|
arg.RentalRate,
|
|
arg.Length,
|
|
arg.ReplacementCost,
|
|
arg.Rating,
|
|
arg.SpecialFeatures,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateFilmPut = `-- name: UpdateFilmPut :execrows
|
|
UPDATE film SET
|
|
title = ?,
|
|
description = ?,
|
|
release_year = ?,
|
|
language_id = ?,
|
|
original_language_id = ?,
|
|
rental_duration = ?,
|
|
rental_rate = ?,
|
|
length = ?,
|
|
replacement_cost = ?,
|
|
rating = ?,
|
|
special_features = ?
|
|
WHERE film_id = ?
|
|
`
|
|
|
|
type UpdateFilmPutParams struct {
|
|
Title string `json:"title"`
|
|
Description interface{} `json:"description"`
|
|
ReleaseYear sql.NullString `json:"release_year"`
|
|
LanguageID int64 `json:"language_id"`
|
|
OriginalLanguageID sql.NullInt64 `json:"original_language_id"`
|
|
RentalDuration int64 `json:"rental_duration"`
|
|
RentalRate float64 `json:"rental_rate"`
|
|
Length sql.NullInt64 `json:"length"`
|
|
ReplacementCost float64 `json:"replacement_cost"`
|
|
Rating sql.NullString `json:"rating"`
|
|
SpecialFeatures sql.NullString `json:"special_features"`
|
|
FilmID int64 `json:"film_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateFilmPut(ctx context.Context, arg UpdateFilmPutParams) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, updateFilmPut,
|
|
arg.Title,
|
|
arg.Description,
|
|
arg.ReleaseYear,
|
|
arg.LanguageID,
|
|
arg.OriginalLanguageID,
|
|
arg.RentalDuration,
|
|
arg.RentalRate,
|
|
arg.Length,
|
|
arg.ReplacementCost,
|
|
arg.Rating,
|
|
arg.SpecialFeatures,
|
|
arg.FilmID,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const patchFilm = `-- name: PatchFilm :execrows
|
|
UPDATE film SET
|
|
title = COALESCE(?1, title),
|
|
description = COALESCE(?2, description),
|
|
release_year = COALESCE(?3, release_year),
|
|
language_id = COALESCE(?4, language_id),
|
|
original_language_id = COALESCE(?5, original_language_id),
|
|
rental_duration = COALESCE(?6, rental_duration),
|
|
rental_rate = COALESCE(?7, rental_rate),
|
|
length = COALESCE(?8, length),
|
|
replacement_cost = COALESCE(?9, replacement_cost),
|
|
rating = COALESCE(?10, rating),
|
|
special_features = COALESCE(?11, special_features)
|
|
WHERE film_id = ?12
|
|
`
|
|
|
|
type PatchFilmParams struct {
|
|
Title sql.NullString `json:"title"`
|
|
Description interface{} `json:"description"`
|
|
ReleaseYear sql.NullString `json:"release_year"`
|
|
LanguageID sql.NullInt64 `json:"language_id"`
|
|
OriginalLanguageID sql.NullInt64 `json:"original_language_id"`
|
|
RentalDuration sql.NullInt64 `json:"rental_duration"`
|
|
RentalRate sql.NullFloat64 `json:"rental_rate"`
|
|
Length sql.NullInt64 `json:"length"`
|
|
ReplacementCost sql.NullFloat64 `json:"replacement_cost"`
|
|
Rating sql.NullString `json:"rating"`
|
|
SpecialFeatures sql.NullString `json:"special_features"`
|
|
FilmID int64 `json:"film_id"`
|
|
}
|
|
|
|
func (q *Queries) PatchFilm(ctx context.Context, arg PatchFilmParams) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, patchFilm,
|
|
arg.Title,
|
|
arg.Description,
|
|
arg.ReleaseYear,
|
|
arg.LanguageID,
|
|
arg.OriginalLanguageID,
|
|
arg.RentalDuration,
|
|
arg.RentalRate,
|
|
arg.Length,
|
|
arg.ReplacementCost,
|
|
arg.Rating,
|
|
arg.SpecialFeatures,
|
|
arg.FilmID,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const deleteFilm = `-- name: DeleteFilm :execrows
|
|
DELETE FROM film WHERE film_id = ?
|
|
`
|
|
|
|
func (q *Queries) DeleteFilm(ctx context.Context, filmID int64) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, deleteFilm, filmID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|