mirror of
https://github.com/acepanel/panel.git
synced 2026-02-06 23:37:23 +08:00
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/libtnb/chix"
|
|
|
|
"github.com/acepanel/panel/internal/biz"
|
|
"github.com/acepanel/panel/internal/http/request"
|
|
)
|
|
|
|
type BackupAccountService struct {
|
|
backupAccountRepo biz.BackupAccountRepo
|
|
}
|
|
|
|
func NewBackupAccountService(backupAccount biz.BackupAccountRepo) *BackupAccountService {
|
|
return &BackupAccountService{
|
|
backupAccountRepo: backupAccount,
|
|
}
|
|
}
|
|
|
|
func (s *BackupAccountService) List(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.Paginate](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
accounts, total, err := s.backupAccountRepo.List(req.Page, req.Limit)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, chix.M{
|
|
"total": total,
|
|
"items": accounts,
|
|
})
|
|
}
|
|
|
|
func (s *BackupAccountService) Create(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.BackupAccountCreate](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
account, err := s.backupAccountRepo.Create(r.Context(), req)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, account)
|
|
}
|
|
|
|
func (s *BackupAccountService) Update(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.BackupAccountUpdate](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = s.backupAccountRepo.Update(r.Context(), req); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
func (s *BackupAccountService) Get(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.ID](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
account, err := s.backupAccountRepo.Get(req.ID)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, account)
|
|
}
|
|
|
|
func (s *BackupAccountService) Delete(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.ID](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = s.backupAccountRepo.Delete(r.Context(), req.ID); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, nil)
|
|
}
|