that's all the API create routes! + some admin UI
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
9329aa9f60
commit
494b29def3
11 changed files with 193 additions and 19 deletions
|
@ -41,6 +41,7 @@ func CreateRelease() http.Handler {
|
|||
|
||||
type PostReleaseBody struct {
|
||||
ID string `json:"id"`
|
||||
Visible bool `json:"visible"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ReleaseType model.ReleaseType `json:"type"`
|
||||
|
@ -57,13 +58,27 @@ func CreateRelease() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
if data.ID == "" {
|
||||
http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.Title == "" {
|
||||
http.Error(w, "Release title cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.ReleaseDate.Unix() == 0 {
|
||||
http.Error(w, "Release date cannot be empty or 0\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if global.GetRelease(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s already exists", data.ID), http.StatusBadRequest)
|
||||
http.Error(w, fmt.Sprintf("Release %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var release = model.Release{
|
||||
ID: data.ID,
|
||||
Visible: data.Visible,
|
||||
Title: data.Title,
|
||||
Description: data.Description,
|
||||
ReleaseType: data.ReleaseType,
|
||||
|
@ -88,5 +103,48 @@ func CreateRelease() http.Handler {
|
|||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteRelease() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var releaseID = r.URL.Path[1:]
|
||||
var release = global.GetRelease(releaseID)
|
||||
if release == nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s does not exist\n", releaseID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := controller.DeleteReleaseDB(global.DB, release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
global.Releases = func () []*model.Release {
|
||||
var releases = []*model.Release{}
|
||||
for _, r := range global.Releases {
|
||||
if r.ID == releaseID { continue }
|
||||
releases = append(releases, r)
|
||||
}
|
||||
return releases
|
||||
}()
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(fmt.Sprintf("Release %s has been deleted\n", release.ID)))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue