feat(tag): make private things private

This commit is contained in:
2026-01-07 09:17:15 +01:00
parent f1e0c1c1c2
commit 5af5ab2a0c
3 changed files with 33 additions and 33 deletions

View File

@@ -10,11 +10,11 @@ import (
) )
type Db interface { type Db interface {
Insert(ctx context.Context, tag Tag) (*Tag, error) insert(ctx context.Context, tag Tag) (*Tag, error)
Update(ctx context.Context, tag Tag) (*Tag, error) update(ctx context.Context, tag Tag) (*Tag, error)
Delete(ctx context.Context, userId uuid.UUID, id uuid.UUID) error delete(ctx context.Context, userId uuid.UUID, id uuid.UUID) error
Get(ctx context.Context, userId uuid.UUID, id uuid.UUID) (*Tag, error) get(ctx context.Context, userId uuid.UUID, id uuid.UUID) (*Tag, error)
GetAll(ctx context.Context, userId uuid.UUID) ([]Tag, error) getAll(ctx context.Context, userId uuid.UUID) ([]Tag, error)
} }
type DbSqlite struct { type DbSqlite struct {
@@ -25,7 +25,7 @@ func NewDbSqlite(db *sqlx.DB) *DbSqlite {
return &DbSqlite{db: db} return &DbSqlite{db: db}
} }
func (db DbSqlite) Insert(ctx context.Context, tag Tag) (*Tag, error) { func (db DbSqlite) insert(ctx context.Context, tag Tag) (*Tag, error) {
r, err := db.db.ExecContext(ctx, ` r, err := db.db.ExecContext(ctx, `
INSERT INTO tag (id, user_id, name, created_at, created_by, updated_at, updated_by) INSERT INTO tag (id, user_id, name, created_at, created_by, updated_at, updated_by)
VALUES (?, ?, ?, ?, ?, ?, ?)`, VALUES (?, ?, ?, ?, ?, ?, ?)`,
@@ -36,10 +36,10 @@ func (db DbSqlite) Insert(ctx context.Context, tag Tag) (*Tag, error) {
return nil, core.ErrInternal return nil, core.ErrInternal
} }
return db.Get(ctx, tag.UserId, tag.Id) return db.get(ctx, tag.UserId, tag.Id)
} }
func (db DbSqlite) Update(ctx context.Context, tag Tag) (*Tag, error) { func (db DbSqlite) update(ctx context.Context, tag Tag) (*Tag, error) {
_, err := db.db.ExecContext(ctx, ` _, err := db.db.ExecContext(ctx, `
UPDATE tag UPDATE tag
SET name = ?, SET name = ?,
@@ -54,10 +54,10 @@ func (db DbSqlite) Update(ctx context.Context, tag Tag) (*Tag, error) {
return nil, core.ErrInternal return nil, core.ErrInternal
} }
return db.Get(ctx, tag.UserId, tag.Id) return db.get(ctx, tag.UserId, tag.Id)
} }
func (db DbSqlite) Delete(ctx context.Context, userId uuid.UUID, id uuid.UUID) error { func (db DbSqlite) delete(ctx context.Context, userId uuid.UUID, id uuid.UUID) error {
r, err := db.db.ExecContext( r, err := db.db.ExecContext(
ctx, ctx,
"DELETE FROM tag WHERE user_id = ? AND id = ?", "DELETE FROM tag WHERE user_id = ? AND id = ?",
@@ -71,7 +71,7 @@ func (db DbSqlite) Delete(ctx context.Context, userId uuid.UUID, id uuid.UUID) e
return nil return nil
} }
func (db DbSqlite) Get(ctx context.Context, userId uuid.UUID, id uuid.UUID) (*Tag, error) { func (db DbSqlite) get(ctx context.Context, userId uuid.UUID, id uuid.UUID) (*Tag, error) {
var tag Tag var tag Tag
err := db.db.Get(&tag, "SELECT * FROM tag WHERE id = ? AND user_id = ?", id, userId) err := db.db.Get(&tag, "SELECT * FROM tag WHERE id = ? AND user_id = ?", id, userId)
@@ -83,7 +83,7 @@ func (db DbSqlite) Get(ctx context.Context, userId uuid.UUID, id uuid.UUID) (*Ta
return &tag, nil return &tag, nil
} }
func (db DbSqlite) GetAll(ctx context.Context, userId uuid.UUID) ([]Tag, error) { func (db DbSqlite) getAll(ctx context.Context, userId uuid.UUID) ([]Tag, error) {
var tags []Tag var tags []Tag
err := db.db.Select(&tags, "SELECT * FROM tag WHERE user_id = ?", userId) err := db.db.Select(&tags, "SELECT * FROM tag WHERE user_id = ?", userId)

View File

@@ -46,7 +46,7 @@ func (h HandlerImpl) handlePage() http.HandlerFunc {
return return
} }
tags, err := h.s.GetAll(r.Context(), user) tags, err := h.s.getAll(r.Context(), user)
if err != nil { if err != nil {
h.r.RenderLayout(r, w, core.ErrorComp(err), user) h.r.RenderLayout(r, w, core.ErrorComp(err), user)
return return
@@ -88,7 +88,7 @@ func (h HandlerImpl) handleEdit() http.HandlerFunc {
return return
} }
tag, err := h.s.Get(r.Context(), user, id) tag, err := h.s.get(r.Context(), user, id)
if err != nil { if err != nil {
core.HandleError(w, r, fmt.Errorf("could not parse Id: %w", core.ErrBadRequest)) core.HandleError(w, r, fmt.Errorf("could not parse Id: %w", core.ErrBadRequest))
return return
@@ -134,13 +134,13 @@ func (h HandlerImpl) handlePost() http.HandlerFunc {
} }
if idStr == "new" { if idStr == "new" {
_, err = h.s.Add(r.Context(), user, input) _, err = h.s.add(r.Context(), user, input)
if err != nil { if err != nil {
core.HandleError(w, r, err) core.HandleError(w, r, err)
return return
} }
} else { } else {
_, err = h.s.Update(r.Context(), user, input) _, err = h.s.update(r.Context(), user, input)
if err != nil { if err != nil {
core.HandleError(w, r, err) core.HandleError(w, r, err)
return return
@@ -168,7 +168,7 @@ func (h HandlerImpl) handleDelete() http.HandlerFunc {
return return
} }
err = h.s.Delete(r.Context(), user, id) err = h.s.delete(r.Context(), user, id)
if err != nil { if err != nil {
core.HandleError(w, r, err) core.HandleError(w, r, err)
return return

View File

@@ -9,11 +9,11 @@ import (
) )
type Service interface { type Service interface {
Add(ctx context.Context, user *auth_types.User, tag Tag) (*Tag, error) add(ctx context.Context, user *auth_types.User, tag Tag) (*Tag, error)
Update(ctx context.Context, user *auth_types.User, tag Tag) (*Tag, error) update(ctx context.Context, user *auth_types.User, tag Tag) (*Tag, error)
Delete(ctx context.Context, user *auth_types.User, tagId uuid.UUID) error delete(ctx context.Context, user *auth_types.User, tagId uuid.UUID) error
Get(ctx context.Context, user *auth_types.User, tagId uuid.UUID) (*Tag, error) get(ctx context.Context, user *auth_types.User, tagId uuid.UUID) (*Tag, error)
GetAll(ctx context.Context, user *auth_types.User) ([]Tag, error) getAll(ctx context.Context, user *auth_types.User) ([]Tag, error)
} }
type ServiceImpl struct { type ServiceImpl struct {
@@ -30,7 +30,7 @@ func NewService(db Db, random core.Random, clock core.Clock) Service {
} }
} }
func (s ServiceImpl) Add(ctx context.Context, user *auth_types.User, tag Tag) (*Tag, error) { func (s ServiceImpl) add(ctx context.Context, user *auth_types.User, tag Tag) (*Tag, error) {
if user == nil { if user == nil {
return nil, core.ErrUnauthorized return nil, core.ErrUnauthorized
} }
@@ -50,15 +50,15 @@ func (s ServiceImpl) Add(ctx context.Context, user *auth_types.User, tag Tag) (*
tag.CreatedBy = user.Id tag.CreatedBy = user.Id
tag.CreatedAt = s.clock.Now() tag.CreatedAt = s.clock.Now()
return s.db.Insert(ctx, tag) return s.db.insert(ctx, tag)
} }
func (s ServiceImpl) Update(ctx context.Context, user *auth_types.User, input Tag) (*Tag, error) { func (s ServiceImpl) update(ctx context.Context, user *auth_types.User, input Tag) (*Tag, error) {
if user == nil { if user == nil {
return nil, core.ErrUnauthorized return nil, core.ErrUnauthorized
} }
tag, err := s.Get(ctx, user, input.Id) tag, err := s.get(ctx, user, input.Id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -78,31 +78,31 @@ func (s ServiceImpl) Update(ctx context.Context, user *auth_types.User, input Ta
now := s.clock.Now() now := s.clock.Now()
tag.UpdatedAt = &now tag.UpdatedAt = &now
return s.db.Update(ctx, *tag) return s.db.update(ctx, *tag)
} }
func (s ServiceImpl) Delete(ctx context.Context, user *auth_types.User, tagId uuid.UUID) error { func (s ServiceImpl) delete(ctx context.Context, user *auth_types.User, tagId uuid.UUID) error {
if user == nil { if user == nil {
return core.ErrUnauthorized return core.ErrUnauthorized
} }
return s.db.Delete(ctx, user.Id, tagId) return s.db.delete(ctx, user.Id, tagId)
} }
func (s ServiceImpl) Get(ctx context.Context, user *auth_types.User, tagId uuid.UUID) (*Tag, error) { func (s ServiceImpl) get(ctx context.Context, user *auth_types.User, tagId uuid.UUID) (*Tag, error) {
if user == nil { if user == nil {
return nil, core.ErrUnauthorized return nil, core.ErrUnauthorized
} }
return s.db.Get(ctx, user.Id, tagId) return s.db.get(ctx, user.Id, tagId)
} }
func (s ServiceImpl) GetAll(ctx context.Context, user *auth_types.User) ([]Tag, error) { func (s ServiceImpl) getAll(ctx context.Context, user *auth_types.User) ([]Tag, error) {
if user == nil { if user == nil {
return nil, core.ErrUnauthorized return nil, core.ErrUnauthorized
} }
return s.db.GetAll(ctx, user.Id) return s.db.getAll(ctx, user.Id)
} }
func (s ServiceImpl) isTagValid(tag Tag) bool { func (s ServiceImpl) isTagValid(tag Tag) bool {