Task/07.03 #1
+83
-1
@@ -1,5 +1,87 @@
|
||||
package com.project.movienight.adapters.persistence
|
||||
|
||||
import com.project.movienight.domain.model.FilmLibrary
|
||||
import org.springframework.jdbc.core.JdbcTemplate
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
class FilmLibraryRepository(private val jdbc: JdbcTemplate) {}
|
||||
@Repository
|
||||
class FilmLibraryRepository(private val jdbc: JdbcTemplate) {
|
||||
|
||||
fun findById(id: Int): FilmLibrary? =
|
||||
jdbc.queryForObject(
|
||||
"SELECT * FROM favorites WHERE id = ?",
|
||||
{ rs, _ ->
|
||||
FilmLibrary(
|
||||
id = rs.getInt("id"),
|
||||
userId = rs.getInt("userid"),
|
||||
filmId = rs.getInt("film_id"),
|
||||
comment = rs.getString("comment"),
|
||||
isViewed = rs.getBoolean("is_viewed"),
|
||||
)
|
||||
},
|
||||
id
|
||||
)
|
||||
|
||||
fun findAllByUserId(userId: Int): List<FilmLibrary> =
|
||||
jdbc.query(
|
||||
"SELECT * FROM favorites WHERE userid = ?",
|
||||
{ rs, _ ->
|
||||
FilmLibrary(
|
||||
id = rs.getInt("id"),
|
||||
userId = rs.getInt("userid"),
|
||||
filmId = rs.getInt("film_id"),
|
||||
comment = rs.getString("comment"),
|
||||
isViewed = rs.getBoolean("is_viewed"),
|
||||
)
|
||||
},
|
||||
userId
|
||||
)
|
||||
|
||||
fun findAll(): List<FilmLibrary> =
|
||||
jdbc.query(
|
||||
"SELECT * FROM favorites"
|
||||
) { rs, _ ->
|
||||
FilmLibrary(
|
||||
id = rs.getInt("id"),
|
||||
userId = rs.getInt("userid"),
|
||||
filmId = rs.getInt("film_id"),
|
||||
comment = rs.getString("comment"),
|
||||
isViewed = rs.getBoolean("is_viewed"),
|
||||
)
|
||||
}
|
||||
|
||||
fun findTopN(limit: Int, sortBy: String = "id"): List<FilmLibrary> =
|
||||
jdbc.query(
|
||||
"SELECT * FROM favorites ORDER BY $sortBy LIMIT ?",
|
||||
{ rs, _ ->
|
||||
FilmLibrary(
|
||||
id = rs.getInt("id"),
|
||||
userId = rs.getInt("userid"),
|
||||
filmId = rs.getInt("film_id"),
|
||||
comment = rs.getString("comment"),
|
||||
isViewed = rs.getBoolean("is_viewed"),
|
||||
)
|
||||
},
|
||||
limit
|
||||
)
|
||||
|
||||
fun save(library: FilmLibrary) {
|
||||
jdbc.update(
|
||||
"""
|
||||
INSERT INTO favorites (userid, film_id, comment, is_viewed)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET comment = ?, is_viewed = ?
|
||||
""",
|
||||
library.userId, library.filmId, library.comment, library.isViewed,
|
||||
library.comment, library.isViewed
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteById(id: Int) {
|
||||
jdbc.update(
|
||||
"DELETE FROM favorites WHERE id = ?",
|
||||
id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,22 +3,23 @@ package com.project.movienight.adapters.persistence
|
||||
import com.project.movienight.domain.model.Film
|
||||
import org.springframework.jdbc.core.JdbcTemplate
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.util.UUID
|
||||
import java.time.LocalDate
|
||||
|
||||
@Repository
|
||||
class FilmRepository(private val jdbc: JdbcTemplate) {
|
||||
|
||||
fun findById(id: UUID): Film? =
|
||||
fun findById(id: Int): Film? =
|
||||
jdbc.queryForObject(
|
||||
"SELECT * FROM films WHERE id = ?",
|
||||
{ rs, _ ->
|
||||
Film(
|
||||
id = UUID.fromString(rs.getString("id")),
|
||||
id = rs.getInt("id"),
|
||||
title = rs.getString("title"),
|
||||
description = rs.getString("description"),
|
||||
genreId = rs.getInt("genre_id"),
|
||||
issueDate = rs.getDate("issue_date")?.toLocalDate(),
|
||||
)
|
||||
},
|
||||
id.toString()
|
||||
id
|
||||
)
|
||||
|
||||
fun findAll(): List<Film> =
|
||||
@@ -26,9 +27,10 @@ class FilmRepository(private val jdbc: JdbcTemplate) {
|
||||
"SELECT * FROM films"
|
||||
) { rs, _ ->
|
||||
Film(
|
||||
id = UUID.fromString(rs.getString("id")),
|
||||
id = rs.getInt("id"),
|
||||
title = rs.getString("title"),
|
||||
description = rs.getString("description"),
|
||||
genreId = rs.getInt("genre_id"),
|
||||
issueDate = rs.getDate("issue_date")?.toLocalDate(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -37,9 +39,10 @@ class FilmRepository(private val jdbc: JdbcTemplate) {
|
||||
"SELECT * FROM films ORDER BY $sortBy LIMIT ?",
|
||||
{ rs, _ ->
|
||||
Film(
|
||||
id = UUID.fromString(rs.getString("id")),
|
||||
id = rs.getInt("id"),
|
||||
title = rs.getString("title"),
|
||||
description = rs.getString("description"),
|
||||
genreId = rs.getInt("genre_id"),
|
||||
issueDate = rs.getDate("issue_date")?.toLocalDate(),
|
||||
)
|
||||
},
|
||||
limit
|
||||
@@ -48,20 +51,20 @@ class FilmRepository(private val jdbc: JdbcTemplate) {
|
||||
fun save(film: Film) {
|
||||
jdbc.update(
|
||||
"""
|
||||
INSERT INTO films (id, title, description)
|
||||
INSERT INTO films (title, genre_id, issue_date)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET title = ?, description = ?
|
||||
SET title = ?, genre_id = ?, issue_date = ?
|
||||
""",
|
||||
film.id.toString(), film.title, film.description,
|
||||
film.title, film.description
|
||||
film.title, film.genreId, film.issueDate,
|
||||
film.title, film.genreId, film.issueDate
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteById(id: UUID) {
|
||||
fun deleteById(id: Int) {
|
||||
jdbc.update(
|
||||
"DELETE FROM films WHERE id = ?",
|
||||
id.toString()
|
||||
id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,83 @@
|
||||
package com.project.movienight.adapters.persistence
|
||||
|
||||
class UserRepository {
|
||||
import com.project.movienight.domain.model.Film
|
||||
import com.project.movienight.domain.model.User
|
||||
import org.springframework.jdbc.core.JdbcTemplate
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.util.UUID
|
||||
|
||||
@Repository
|
||||
class UserRepository(private val jdbc: JdbcTemplate) {
|
||||
|
||||
fun findById(id: UUID): User? =
|
||||
jdbc.queryForObject(
|
||||
"SELECT * FROM users WHERE id = ?",
|
||||
{rs, _ ->
|
||||
User(
|
||||
id = UUID.fromString(rs.getString("id")),
|
||||
name = rs.getString("name"),
|
||||
email = rs.getString("email"),
|
||||
library = null // брать библиотеку пользователя лучше отдельным запросом,
|
||||
// который будет в FilmLibraryRepository
|
||||
)
|
||||
},
|
||||
id.toString()
|
||||
)
|
||||
|
||||
fun save(user: User) {
|
||||
jdbc.update(
|
||||
"""
|
||||
INSERT INTO users(id, name, email, library)
|
||||
VALUES (:id, :name, :email, :library)
|
||||
ON CONFLICT (id)
|
||||
DO UPDATE SET name = :name, email = :email, library = :library
|
||||
""",
|
||||
user.id.toString(), user.name, user.email, user.library,
|
||||
user.name, user.email, user.library
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteById(id: UUID) {
|
||||
jdbc.update(
|
||||
"DELETE FROM users WHERE id = ?",
|
||||
id.toString()
|
||||
)
|
||||
}
|
||||
|
||||
fun findByEmail(email: String): User? =
|
||||
jdbc.queryForObject(
|
||||
"SELECT * FROM users WHERE email = ?",
|
||||
{rs, _ ->
|
||||
User(
|
||||
id = UUID.fromString(rs.getString("id")),
|
||||
name = rs.getString("name"),
|
||||
email = rs.getString("email"),
|
||||
library = null
|
||||
)
|
||||
},
|
||||
|
||||
)
|
||||
fun findTopN(limit: Int, sortBy: String = "name"): List<User> =
|
||||
jdbc.query(
|
||||
"SELECT * FROM users ORDER BY $sortBy LIMIT ?",
|
||||
{ rs, _ ->
|
||||
User(
|
||||
id = UUID.fromString(rs.getString("id")),
|
||||
name = rs.getString("name"),
|
||||
email = rs.getString("email"),
|
||||
library = null
|
||||
)
|
||||
},
|
||||
limit
|
||||
)
|
||||
|
||||
fun existsByEmail(email: String): Boolean {
|
||||
val count = jdbc.queryForObject(
|
||||
"SELECT COUNT(*) FROM users WHERE email = ?",
|
||||
Int::class.java,
|
||||
email
|
||||
) ?: 0
|
||||
return count > 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.project.movienight.domain.model
|
||||
|
||||
import java.util.UUID
|
||||
import java.time.LocalDate
|
||||
|
||||
data class Film(
|
||||
val id: UUID,
|
||||
val id: Int,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val genreId: Int,
|
||||
val issueDate: LocalDate?,
|
||||
)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.project.movienight.domain.model
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
data class FilmLibrary(
|
||||
val id: UUID,
|
||||
val userId: UUID,
|
||||
val id: Int,
|
||||
val userId: Int,
|
||||
val filmId: Int,
|
||||
val comment: String?,
|
||||
val isViewed: Boolean,
|
||||
)
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
-- DROP SCHEMA public;
|
||||
-- CREATE SCHEMA public AUTHORIZATION pg_database_owner;
|
||||
-- COMMENT ON SCHEMA public IS 'standard public schema';
|
||||
|
||||
-- public.genres определение
|
||||
-- Drop table
|
||||
DROP TABLE if exists public.genres;
|
||||
CREATE TABLE public.genres (
|
||||
id int4 GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
"name" varchar NULL,
|
||||
CONSTRAINT genres_unique PRIMARY KEY (id)
|
||||
);
|
||||
|
||||
|
||||
-- public.users определение
|
||||
-- Drop table
|
||||
DROP TABLE if exists public.users;
|
||||
CREATE TABLE public.users (
|
||||
id int4 GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
login varchar NOT NULL,
|
||||
"password" varchar NOT NULL,
|
||||
CONSTRAINT users_unique UNIQUE (id)
|
||||
);
|
||||
|
||||
|
||||
-- public.films определение
|
||||
-- Drop table
|
||||
DROP TABLE if exists public.films;
|
||||
CREATE TABLE public.films (
|
||||
id int4 GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
title varchar NOT NULL,
|
||||
genreid int4 NOT NULL,
|
||||
issuedate date NULL,
|
||||
CONSTRAINT films_unique UNIQUE(id)
|
||||
);
|
||||
|
||||
DROP TABLE if exists public.fiml_genres;
|
||||
CREATE TABLE public.film_genres (
|
||||
id int4 GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
genreid int4 NOT NULL,
|
||||
filmid int4 NOT NULL,
|
||||
CONSTRAINT film_genres_genre_fk FOREIGN KEY (genreid) REFERENCES public.genres(id),
|
||||
CONSTRAINT film_genres_film_fk FOREIGN KEY (filmid) REFERENCES public.films(id)
|
||||
);
|
||||
|
||||
|
||||
-- Drop table
|
||||
DROP TABLE if exists public.favorites;
|
||||
CREATE TABLE public.favorites (
|
||||
id int4 GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
userid int4 NULL,
|
||||
filmid int4 NULL,
|
||||
"comment" varchar NULL,
|
||||
isviewed bool NULL,
|
||||
CONSTRAINT favorites_unique PRIMARY KEY (id),
|
||||
CONSTRAINT favorites_films_fk FOREIGN KEY (filmid) REFERENCES public.films(id),
|
||||
CONSTRAINT favorites_users_fk FOREIGN KEY (userid) REFERENCES public.users(id)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user