This commit is contained in:
2026-08-26 11:01:46 +03:00
parent 34a798d345
commit 4bd1512994
712 changed files with 538122 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
package db
import (
"context"
"github.com/gocraft/dbr/v2"
)
type Storage struct {
conn *dbr.Connection
}
func New(conn *dbr.Connection) *Storage {
s := Storage{conn}
s.init()
return &s
}
func (s *Storage) init() {
for _, q := range []string{
"CREATE TABLE IF NOT EXISTS users(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, login VARCHAR(200) UNIQUE , password VARCHAR(200))",
"CREATE TABLE IF NOT EXISTS video(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INTEGER, description VARCHAR(500), private BOOLEAN, link VARCHAR(400))",
"CREATE TABLE IF NOT EXISTS access(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INTEGER, video_id INTEGER)",
} {
_, err := s.conn.Exec(q)
if err != nil {
panic(err)
}
}
}
func (s *Storage) InsertUser(ctx context.Context, user *User) error {
sess := s.conn.NewSession(nil)
_, err := sess.InsertInto("users").Columns("login", "password").Record(user).ExecContext(ctx)
return err
}
func (s *Storage) FindUser(ctx context.Context, login, password string) (*User, error) {
sess := s.conn.NewSession(nil)
user := new(User)
err := sess.Select("*").
From("users").
Where("login = ?", login).
Where("password = ?", password).
LoadOneContext(ctx, user)
if err != nil {
return nil, err
}
return user, err
}
func (s *Storage) AddVideo(ctx context.Context, v *Video) error {
sess := s.conn.NewSession(nil)
_, err := sess.InsertInto("video").
Columns("user_id", "description", "private", "link").
Record(v).ExecContext(ctx)
return err
}
func (s *Storage) ListVideo(ctx context.Context, limit int) (v []Video, err error) {
sess := s.conn.NewSession(nil)
_, err = sess.Select("*").
From("video").
OrderDesc("id").
Limit(uint64(limit)).
LoadContext(ctx, &v)
return
}
func (s *Storage) ListUserVideo(ctx context.Context, userId int64) (v []Video, err error) {
sess := s.conn.NewSession(nil)
_, err = sess.Select("*").
From("video").
Where("user_id = ?", userId).
OrderDesc("id").LoadContext(ctx, &v)
return
}
func (s *Storage) GetVideo(ctx context.Context, id interface{}) (*Video, error) {
sess := s.conn.NewSession(nil)
v := new(Video)
err := sess.Select("*").From("video").Where("id = ?", id).LoadOneContext(ctx, v)
return v, err
}
func (s *Storage) HaveAccess(ctx context.Context, id interface{}, userId int64) bool {
sess := s.conn.NewSession(nil)
var c int
err := sess.Select("count(*)").
From("access").
Where("user_id = ?", userId).
Where("video_id = ?", id).
LoadOneContext(ctx, &c)
if err != nil {
return false
}
return c > 0
}
func (s *Storage) AddAccess(ctx context.Context, id int, userId int64) error {
sess := s.conn.NewSession(nil)
_, err := sess.InsertInto("access").
Pair("video_id", id).
Pair("user_id", userId).
ExecContext(ctx)
return err
}
+172
View File
@@ -0,0 +1,172 @@
package db
import (
"context"
"github.com/gocraft/dbr/v2"
_ "github.com/mattn/go-sqlite3"
"testing"
)
func setup() (*Storage, func()) {
conn, err := dbr.Open("sqlite3", ":memory:", nil)
if err != nil {
panic(err)
}
return New(conn), func() {
conn.Close()
}
}
func TestStorage_InsertUser(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
u := User{Login: "user", Password: "test"}
err := db.InsertUser(ctx, &u)
if err != nil {
t.Error("failed to insert user", err.Error())
}
if u.ID <= 0 {
t.Error("id should be positive")
}
err = db.InsertUser(ctx, &u)
if err == nil {
t.Error("should fail when inserting same user")
}
}
func TestStorage_FindUser(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
u := User{Login: "user", Password: "test"}
err := db.InsertUser(ctx, &u)
if err != nil {
t.Fatal("failed to insert user", err.Error())
}
u1, err := db.FindUser(ctx, "user", "test")
if err != nil {
t.Error("failed to retrieve user", err.Error())
}
if u1.Password != u.Password || u1.Login != u.Login {
t.Error("users should be equal")
}
u2, err := db.FindUser(ctx, "test", "test")
if err == nil {
t.Error("should fail because no user not found")
}
if u2 != nil {
t.Error("should be nil if user not found")
}
}
func TestStorage_AddVideo(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
v := Video{
UserID: 1,
Description: "Test",
Private: false,
Link: "test",
}
err := db.AddVideo(ctx, &v)
if err != nil {
t.Error("failed to insert video")
}
if v.ID <= 0 {
t.Error("id should be positive")
}
}
func TestStorage_ListVideo(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
v := Video{
UserID: 1,
Description: "Test",
Private: false,
Link: "test",
}
err := db.AddVideo(ctx, &v)
if err != nil {
t.Fatal("failed to insert video")
}
vds, err := db.ListVideo(ctx, 1)
if err != nil || len(vds) < 1 {
t.Error("failed to retrieve videos from db")
}
v.ID = vds[0].ID
if vds[0] != v {
t.Error("v != res[0]")
}
}
func TestStorage_ListUserVideo(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
for _, v := range []Video{
{UserID: 1, Description: "Test", Private: false, Link: "test"},
{UserID: 2, Description: "Test2", Private: false, Link: "test2"}} {
if err := db.AddVideo(ctx, &v); err != nil {
t.Fatal("failed to insert video:", err)
}
}
for _, u := range []int64{1, 2} {
vds, err := db.ListUserVideo(ctx, int64(u))
if err != nil || len(vds) != 1 {
t.Error("failed to retrieve videos from db for user: ", u)
}
if vds[0].UserID != u {
t.Errorf("video with wrong uid found. Expected %d, found %d", u, vds[0].UserID)
}
}
}
func TestStorage_GetVideo(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
v := Video{
UserID: 1,
Description: "Test",
Private: false,
Link: "test",
}
err := db.AddVideo(ctx, &v)
if err != nil {
t.Fatal("failed to insert video")
}
v2, err := db.GetVideo(ctx, v.ID)
if err != nil {
t.Errorf("failed to get video %v", err.Error())
}
if *v2 != v {
t.Errorf("videos dont match: expected %v, found %v", v, v2)
}
}
func TestStorage_AddAccess_HavAccess(t *testing.T) {
db, teardown := setup()
defer teardown()
ctx := context.Background()
if db.HaveAccess(ctx, 1, 2) {
t.Error("should not have access: empty table")
}
if err := db.AddAccess(ctx, 1, 2); err != nil {
t.Error("failed to add access", err)
}
if !db.HaveAccess(ctx, 1, 2) {
t.Error("should have access")
}
}
+15
View File
@@ -0,0 +1,15 @@
package db
type User struct {
ID int64
Login string
Password string
}
type Video struct {
ID int64
UserID int64
Description string
Private bool
Link string
}