tugas tabel user
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"5803025020/w04/internal/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type SessionStore struct {
|
||||
conn *pgx.Conn
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func NewSessionStore(conn *pgx.Conn, optMu ...*sync.Mutex) *SessionStore {
|
||||
var mu *sync.Mutex
|
||||
if len(optMu) > 0 && optMu[0] != nil {
|
||||
mu = optMu[0]
|
||||
} else {
|
||||
mu = &sync.Mutex{}
|
||||
}
|
||||
return &SessionStore{conn: conn, mu: mu}
|
||||
}
|
||||
|
||||
func (s *SessionStore) Create(ctx context.Context, session *models.Session) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if session.CreatedAt.IsZero() {
|
||||
session.CreatedAt = time.Now()
|
||||
}
|
||||
|
||||
query := `INSERT INTO sessions (token, user_id, created_at) VALUES ($1, $2, $3)`
|
||||
_, err := s.conn.Exec(ctx, query, session.Token, session.UserID, session.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SessionStore) Get(ctx context.Context, token string) (*models.Session, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
var sess models.Session
|
||||
query := `SELECT token, user_id, created_at FROM sessions WHERE token = $1`
|
||||
err := s.conn.QueryRow(ctx, query, token).Scan(&sess.Token, &sess.UserID, &sess.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sess, nil
|
||||
}
|
||||
|
||||
func (s *SessionStore) Delete(ctx context.Context, token string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
query := `DELETE FROM sessions WHERE token = $1`
|
||||
_, err := s.conn.Exec(ctx, query, token)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"5803025020/w04/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type SnippetStore struct {
|
||||
conn *pgx.Conn
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func NewSnippetStore(conn *pgx.Conn, optMu ...*sync.Mutex) *SnippetStore {
|
||||
var mu *sync.Mutex
|
||||
if len(optMu) > 0 && optMu[0] != nil {
|
||||
mu = optMu[0]
|
||||
} else {
|
||||
mu = &sync.Mutex{}
|
||||
}
|
||||
return &SnippetStore{conn: conn, mu: mu}
|
||||
}
|
||||
|
||||
func (s *SnippetStore) Create(ctx context.Context, snippet *models.Snippet) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if snippet.ID == "" {
|
||||
snippet.ID = uuid.New().String()
|
||||
}
|
||||
|
||||
query := `INSERT INTO snippets (id, title, content, user_id) VALUES ($1, $2, $3, $4)`
|
||||
_, err := s.conn.Exec(ctx, query, snippet.ID, snippet.Title, snippet.Content, snippet.UserID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SnippetStore) GetByID(ctx context.Context, id string) (*models.Snippet, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
var sn models.Snippet
|
||||
query := `SELECT id, title, content, user_id FROM snippets WHERE id = $1`
|
||||
err := s.conn.QueryRow(ctx, query, id).Scan(&sn.ID, &sn.Title, &sn.Content, &sn.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sn, nil
|
||||
}
|
||||
|
||||
func (s *SnippetStore) GetAll(ctx context.Context) ([]*models.Snippet, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
query := `SELECT id, title, content, user_id FROM snippets`
|
||||
rows, err := s.conn.Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
snippets := make([]*models.Snippet, 0)
|
||||
for rows.Next() {
|
||||
var sn models.Snippet
|
||||
if err := rows.Scan(&sn.ID, &sn.Title, &sn.Content, &sn.UserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
snippets = append(snippets, &sn)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return snippets, nil
|
||||
}
|
||||
|
||||
func (s *SnippetStore) Delete(ctx context.Context, id string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
query := `DELETE FROM snippets WHERE id = $1`
|
||||
_, err := s.conn.Exec(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"5803025020/w04/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type UserStore struct {
|
||||
conn *pgx.Conn
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func NewUserStore(conn *pgx.Conn, optMu ...*sync.Mutex) *UserStore {
|
||||
var mu *sync.Mutex
|
||||
if len(optMu) > 0 && optMu[0] != nil {
|
||||
mu = optMu[0]
|
||||
} else {
|
||||
mu = &sync.Mutex{}
|
||||
}
|
||||
return &UserStore{conn: conn, mu: mu}
|
||||
}
|
||||
|
||||
func (s *UserStore) Create(ctx context.Context, user *models.User) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if user.ID == "" {
|
||||
user.ID = uuid.New().String()
|
||||
}
|
||||
|
||||
query := `INSERT INTO users (id, username, password_hash) VALUES ($1, $2, $3)`
|
||||
_, err := s.conn.Exec(ctx, query, user.ID, user.Username, user.PasswordHash)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetByUsername(ctx context.Context, username string) (*models.User, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
var u models.User
|
||||
query := `SELECT id, username, password_hash FROM users WHERE username = $1`
|
||||
err := s.conn.QueryRow(ctx, query, username).Scan(&u.ID, &u.Username, &u.PasswordHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (s *UserStore) GetByID(ctx context.Context, id string) (*models.User, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
var u models.User
|
||||
query := `SELECT id, username, password_hash FROM users WHERE id = $1`
|
||||
err := s.conn.QueryRow(ctx, query, id).Scan(&u.ID, &u.Username, &u.PasswordHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
Reference in New Issue
Block a user