added registeration and started user login
This commit is contained in:
parent
3f2cbcea33
commit
7ec8dbc211
14 changed files with 327 additions and 1 deletions
|
@ -20,6 +20,7 @@ func NewStore(dataSourceName string) (*Store, error) {
|
|||
ThreadStore: &ThreadStore{DB: db},
|
||||
PostStore: &PostStore{DB: db},
|
||||
CommentStore: &CommentStore{DB: db},
|
||||
UserStore: &UserStore{DB: db},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -27,4 +28,5 @@ type Store struct {
|
|||
*ThreadStore
|
||||
*PostStore
|
||||
*CommentStore
|
||||
*UserStore
|
||||
}
|
||||
|
|
64
postgres/user_store.go
Normal file
64
postgres/user_store.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
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
|
||||
}
|
Reference in a new issue