Add updateUser and deleteUser API client methods with PATCH and DELETE endpoints. Add updatePolicy and deletePolicy API client methods. Add email field to User type. Add Actions column to users and policies tables with Edit and Delete buttons. Implement inline edit forms for users and policies with state management for editing mode. Add update and delete mutations with query invalidation on success. Add error notices
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"nexavpn/backend/internal/auth"
|
|
)
|
|
|
|
type Service struct {
|
|
repo Repository
|
|
}
|
|
|
|
func NewService(repo Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) List(ctx context.Context) ([]User, error) {
|
|
return s.repo.List(ctx)
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, input CreateRequest) (User, error) {
|
|
passwordHash, err := auth.HashPassword(input.Password)
|
|
if err != nil {
|
|
return User{}, err
|
|
}
|
|
return s.repo.Create(ctx, input, passwordHash)
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, userID string, input UpdateRequest) (User, error) {
|
|
id, err := uuid.Parse(userID)
|
|
if err != nil {
|
|
return User{}, err
|
|
}
|
|
|
|
var passwordHash *string
|
|
if input.Password != nil && *input.Password != "" {
|
|
hashed, err := auth.HashPassword(*input.Password)
|
|
if err != nil {
|
|
return User{}, err
|
|
}
|
|
passwordHash = &hashed
|
|
}
|
|
|
|
return s.repo.Update(ctx, id, input, passwordHash)
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, userID string) error {
|
|
id, err := uuid.Parse(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.repo.Delete(ctx, id)
|
|
}
|
|
|
|
func (s *Service) SetActive(ctx context.Context, userID string, active bool) error {
|
|
id, err := uuid.Parse(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.repo.SetActive(ctx, id, active)
|
|
}
|