autobrr/internal/user/service.go
KaiserBh df2612602b
feat(auth): change password and username (#1295)
* feat(backend): added change password api endpoint.

* feat(web): added profile UI to change password.

I think we can change the username too, but I don't know if we should for now disabled the username field.

* refactor: don't leak username or password.

* refactor: protect the route.

* generic

* feat: add ChangeUsername

* fix(tests): speculative fix for TestUserRepo_Update

* Revert "feat: add ChangeUsername"

This reverts commit d4c1645002883a278aa45dec3c8c19fa1cc75d9b.

* refactor into 1 endpoint that handles both

* feat: added option to change username as well. :pain:

* refactor: frontend

* refactor: function names in backend

I think this makes it more clear what their function is

* fix: change to 2 cols with separator

* refactor: update user

* fix: test db create user

---------

Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
Co-authored-by: soup <soup@r4tio.dev>
Co-authored-by: martylukyy <35452459+martylukyy@users.noreply.github.com>
Co-authored-by: ze0s <ze0s@riseup.net>
2023-12-26 15:50:57 +01:00

58 lines
1.4 KiB
Go

// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package user
import (
"context"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
)
type Service interface {
GetUserCount(ctx context.Context) (int, error)
FindByUsername(ctx context.Context, username string) (*domain.User, error)
CreateUser(ctx context.Context, req domain.CreateUserRequest) error
Update(ctx context.Context, req domain.UpdateUserRequest) error
}
type service struct {
repo domain.UserRepo
}
func NewService(repo domain.UserRepo) Service {
return &service{
repo: repo,
}
}
func (s *service) GetUserCount(ctx context.Context) (int, error) {
return s.repo.GetUserCount(ctx)
}
func (s *service) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
user, err := s.repo.FindByUsername(ctx, username)
if err != nil {
return nil, err
}
return user, nil
}
func (s *service) CreateUser(ctx context.Context, req domain.CreateUserRequest) error {
userCount, err := s.repo.GetUserCount(ctx)
if err != nil {
return err
}
if userCount > 0 {
return errors.New("only 1 user account is supported at the moment")
}
return s.repo.Store(ctx, req)
}
func (s *service) Update(ctx context.Context, req domain.UpdateUserRequest) error {
return s.repo.Update(ctx, req)
}