Tuas W06
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"5803025020/w04/internal/models"
|
||||
"5803025020/w04/internal/store"
|
||||
)
|
||||
|
||||
// fakeUserStore is a hand-written in-memory implementation of UserStore.
|
||||
type fakeUserStore struct {
|
||||
mu sync.Mutex
|
||||
byName map[string]*models.User
|
||||
}
|
||||
|
||||
func newFakeUserStore() *fakeUserStore {
|
||||
return &fakeUserStore{byName: map[string]*models.User{}}
|
||||
}
|
||||
|
||||
func (f *fakeUserStore) Create(ctx context.Context, user *models.User) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if _, exists := f.byName[user.Username]; exists {
|
||||
return store.ErrUsernameTaken
|
||||
}
|
||||
cp := *user
|
||||
f.byName[user.Username] = &cp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeUserStore) GetByUsername(ctx context.Context, username string) (*models.User, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
u, ok := f.byName[username]
|
||||
if !ok {
|
||||
return nil, store.ErrUserNotFound
|
||||
}
|
||||
cp := *u
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
// fakeSessionStore is a hand-written in-memory implementation of SessionStore.
|
||||
type fakeSessionStore struct {
|
||||
mu sync.Mutex
|
||||
sessions map[string]*models.Session
|
||||
}
|
||||
|
||||
func newFakeSessionStore() *fakeSessionStore {
|
||||
return &fakeSessionStore{sessions: map[string]*models.Session{}}
|
||||
}
|
||||
|
||||
func (f *fakeSessionStore) Create(ctx context.Context, session *models.Session) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
cp := *session
|
||||
f.sessions[session.Token] = &cp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSessionStore) Delete(ctx context.Context, token string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if _, ok := f.sessions[token]; !ok {
|
||||
return store.ErrSessionNotFound
|
||||
}
|
||||
delete(f.sessions, token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSessionStore) has(token string) bool {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
_, ok := f.sessions[token]
|
||||
return ok
|
||||
}
|
||||
|
||||
// fakeSnippetStore is a hand-written in-memory implementation of SnippetStore.
|
||||
type fakeSnippetStore struct {
|
||||
mu sync.Mutex
|
||||
snippets map[string]*models.Snippet
|
||||
}
|
||||
|
||||
func newFakeSnippetStore() *fakeSnippetStore {
|
||||
return &fakeSnippetStore{snippets: map[string]*models.Snippet{}}
|
||||
}
|
||||
|
||||
func (f *fakeSnippetStore) Create(ctx context.Context, snippet *models.Snippet) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
cp := *snippet
|
||||
f.snippets[snippet.ID] = &cp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSnippetStore) GetByID(ctx context.Context, id string) (*models.Snippet, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
sn, ok := f.snippets[id]
|
||||
if !ok {
|
||||
return nil, store.ErrSnippetNotFound
|
||||
}
|
||||
cp := *sn
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
func (f *fakeSnippetStore) GetByUserID(ctx context.Context, userID string) ([]*models.Snippet, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
result := make([]*models.Snippet, 0)
|
||||
for _, sn := range f.snippets {
|
||||
if sn.UserID == userID {
|
||||
cp := *sn
|
||||
result = append(result, &cp)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (f *fakeSnippetStore) Delete(ctx context.Context, id string) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if _, ok := f.snippets[id]; !ok {
|
||||
return store.ErrSnippetNotFound
|
||||
}
|
||||
delete(f.snippets, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSnippetStore) has(id string) bool {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
_, ok := f.snippets[id]
|
||||
return ok
|
||||
}
|
||||
Reference in New Issue
Block a user