refactor: more implementation to refactor and deleted files
This commit is contained in:
parent
ba684bd149
commit
50e6b53091
17 changed files with 84 additions and 945 deletions
|
|
@ -10,6 +10,7 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
type Profile struct {
|
||||
|
|
@ -43,7 +44,7 @@ func (s *Service) GetProfiles() http.HandlerFunc {
|
|||
|
||||
// read file contents
|
||||
var profile *Profile
|
||||
profile, err = LoadProfile(path)
|
||||
profile, err = LoadProfileFromFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -91,7 +92,7 @@ func (s *Service) GetProfile() http.HandlerFunc {
|
|||
err error
|
||||
)
|
||||
|
||||
profile, err = LoadProfile(path)
|
||||
profile, err = LoadProfileFromFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -114,8 +115,8 @@ func (s *Service) GetProfile() http.HandlerFunc {
|
|||
func (s *Service) CreateProfile() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type input struct {
|
||||
path string `json:"path"`
|
||||
profile *Profile `json:"profile"`
|
||||
Path string `json:"path"`
|
||||
Profile *Profile `json:"profile"`
|
||||
}
|
||||
var (
|
||||
body, contents []byte
|
||||
|
|
@ -137,37 +138,85 @@ func (s *Service) CreateProfile() http.HandlerFunc {
|
|||
}
|
||||
|
||||
// serialize just the profile part
|
||||
contents, err = json.Marshal(in.profile)
|
||||
contents, err = json.Marshal(in.Profile)
|
||||
if err != nil {
|
||||
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// create a new profile on disk
|
||||
err = os.WriteFile(in.path, contents, os.ModePerm)
|
||||
err = os.WriteFile(in.Path, contents, os.ModePerm)
|
||||
if err != nil {
|
||||
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) CreateProfileVar() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
func (s *Service) SetProfileData() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
body, contents []byte
|
||||
newContents string
|
||||
profile *Profile
|
||||
path string
|
||||
err error
|
||||
)
|
||||
|
||||
body, err = io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &profile)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// make sure the request data sets an ID
|
||||
if profile.ID == "" {
|
||||
http.Error(w, "ID must be set to a non-empty value", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// read the contents the file with profile ID
|
||||
path = s.BuildProfilePath(profile.ID)
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// modify the data of the profile's contents
|
||||
newContents, err = sjson.Set(string(contents), "data", profile.Data)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// write only the data to the file with ID
|
||||
err = os.WriteFile(path, []byte(newContents), os.ModePerm)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) DeleteProfileVar() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
func (s *Service) DeleteProfileData() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) GetProfileData() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
|
||||
func (s *Service) GetProfileVar() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
|
||||
func (s *Service) CreateProfilePath() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue