This repository has been archived on 2021-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
goddit/postgres/user_store.go

64 lines
1.6 KiB
Go

package postgres
import (
"fmt"
"git.snrd.de/Spaenny/goddit"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
type UserStore struct {
*sqlx.DB
}
func (s *UserStore) User(id uuid.UUID) (goddit.User, error) {
var u goddit.User
if err := s.Get(&u, `SELECT * FROM users WHERE id = $1`, id); err != nil {
return goddit.User{}, fmt.Errorf("error getting user: %w", err)
}
return u, nil
}
func (s *UserStore) UserByUsername(username string) (goddit.User, error) {
var u goddit.User
if err := s.Get(&u, `SELECT * FROM users WHERE username = $1`, username); err != nil {
return goddit.User{}, fmt.Errorf("error getting user: %w", err)
}
return u, nil
}
func (s *UserStore) Users() ([]goddit.User, error) {
var uu []goddit.User
if err := s.Select(&uu, `SELECT * FROM users`); err != nil {
return []goddit.User{}, fmt.Errorf("error getting users: %w", err)
}
return uu, nil
}
func (s *UserStore) CreateUser(u *goddit.User) error {
if err := s.Get(u, `INSERT INTO users VALUES($1, $2, $3) RETURNING *`,
u.ID,
u.Username,
u.Password); err != nil {
return fmt.Errorf("error creating user: %w", err)
}
return nil
}
func (s *UserStore) UpdateUser(u *goddit.User) error {
if err := s.Get(u, `UPDATE INTO users SET username = $1, password = $2 WHERE id = $3) RETURNING *`,
u.Username,
u.Password,
u.ID); err != nil {
return fmt.Errorf("error updating user: %w", err)
}
return nil
}
func (s *UserStore) DeleteUser(id uuid.UUID) error {
if _, err := s.Exec(`DELETE FROM users WHERE id = $1`, id); err != nil {
return fmt.Errorf("error deleteing user: %w", err)
}
return nil
}