|
|
@ -8,10 +8,7 @@ import ( |
|
|
|
"log" |
|
|
|
"mime/multipart" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/JumboPetstore/database" |
|
|
|
"git.tovijaeschke.xyz/tovi/JumboPetstore/models" |
|
|
@ -20,6 +17,13 @@ import ( |
|
|
|
"github.com/gorilla/mux" |
|
|
|
) |
|
|
|
|
|
|
|
func verifyStatus(status string) error { |
|
|
|
if status != "available" && status != "pending" && status != "sold" { |
|
|
|
return errors.New("Invalid status") |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func deserialsePetJson(data []byte) (models.Pet, error) { |
|
|
|
var ( |
|
|
|
petData models.Pet = models.Pet{} |
|
|
@ -42,17 +46,28 @@ func deserialsePetJson(data []byte) (models.Pet, error) { |
|
|
|
} |
|
|
|
|
|
|
|
if len(jsonStructureTestResults.MismatchedFields) > 0 { |
|
|
|
fmt.Println(jsonStructureTestResults.MismatchedFields) |
|
|
|
return petData, errors.New("MismatchedFields found when deserializing data") |
|
|
|
return petData, errors.New(fmt.Sprintf( |
|
|
|
"MismatchedFields found when deserializing data, %s", |
|
|
|
jsonStructureTestResults.Errors().Error(), |
|
|
|
)) |
|
|
|
} |
|
|
|
|
|
|
|
if len(jsonStructureTestResults.MissingFields) > 0 { |
|
|
|
fmt.Println(jsonStructureTestResults.MissingFields) |
|
|
|
return petData, errors.New("MissingFields found when deserializing data") |
|
|
|
return petData, errors.New(fmt.Sprintf( |
|
|
|
"MissingFields found when deserializing data, %s", |
|
|
|
jsonStructureTestResults.Errors().Error(), |
|
|
|
)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Deserialize the JSON into the struct
|
|
|
|
err = json.Unmarshal(data, &petData) |
|
|
|
if err != nil { |
|
|
|
return petData, err |
|
|
|
} |
|
|
|
|
|
|
|
err = verifyStatus(petData.Status) |
|
|
|
|
|
|
|
return petData, err |
|
|
|
} |
|
|
|
|
|
|
@ -102,24 +117,22 @@ func PetHandlerCreateUpdate(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
switch r.Method { |
|
|
|
case "POST": |
|
|
|
petData, err = database.CreatePet(petData) |
|
|
|
err = database.CreatePet(&petData) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
genericJsonReturn(w, 405, "Invalid data", "unknown") |
|
|
|
} |
|
|
|
break |
|
|
|
case "PUT": |
|
|
|
petData, err = database.UpdatePet(petData) |
|
|
|
err = database.UpdatePet(&petData) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
genericJsonReturn(w, 405, "Invalid data", "unknown") |
|
|
|
} |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(petData, "", " ") |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered when creating pet record: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -128,103 +141,122 @@ func PetHandlerCreateUpdate(w http.ResponseWriter, r *http.Request) { |
|
|
|
w.Write(returnJson) |
|
|
|
} |
|
|
|
|
|
|
|
func PetHandlerId(w http.ResponseWriter, r *http.Request) { |
|
|
|
func getPetDataById(w http.ResponseWriter, r *http.Request) (models.Pet, bool) { |
|
|
|
var ( |
|
|
|
petData models.Pet |
|
|
|
urlVars map[string]string |
|
|
|
returnJson []byte |
|
|
|
notFoundJson map[string]interface{} = make(map[string]interface{}) |
|
|
|
id_str string |
|
|
|
id int |
|
|
|
ok bool |
|
|
|
err error |
|
|
|
petData models.Pet |
|
|
|
urlVars map[string]string |
|
|
|
idStr string |
|
|
|
id int |
|
|
|
ok bool |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
urlVars = mux.Vars(r) |
|
|
|
id_str, ok = urlVars["petId"] |
|
|
|
idStr, ok = urlVars["petId"] |
|
|
|
if !ok { |
|
|
|
log.Printf("Error encountered getting id\n") |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return petData, false |
|
|
|
} |
|
|
|
|
|
|
|
id, err = strconv.Atoi(id_str) |
|
|
|
id, err = strconv.Atoi(idStr) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered converting id to string: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return petData, false |
|
|
|
} |
|
|
|
|
|
|
|
petData = database.GetPetById(id) |
|
|
|
if petData.Id == 0 { |
|
|
|
log.Printf("Could not find pet with id %d\n", id) |
|
|
|
w.WriteHeader(http.StatusNotFound) |
|
|
|
notFoundJson["code"] = 404 |
|
|
|
notFoundJson["type"] = "unknown" |
|
|
|
notFoundJson["message"] = "not found" |
|
|
|
returnJson, err = json.MarshalIndent(notFoundJson, "", " ") |
|
|
|
w.Write(returnJson) |
|
|
|
genericJsonReturn(w, 404, "Not found", "unknown") |
|
|
|
return petData, false |
|
|
|
} |
|
|
|
|
|
|
|
return petData, true |
|
|
|
} |
|
|
|
|
|
|
|
func GetPetHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
var ( |
|
|
|
petData models.Pet |
|
|
|
returnJson []byte |
|
|
|
ok bool |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
petData, ok = getPetDataById(w, r) |
|
|
|
if !ok { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
switch r.Method { |
|
|
|
case "GET": |
|
|
|
returnJson, err = json.MarshalIndent(petData, "", " ") |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered when creating pet record: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
} |
|
|
|
returnJson, err = json.MarshalIndent(petData, "", " ") |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered when creating pet record: %s\n", err.Error()) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Return updated json
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
w.Write(returnJson) |
|
|
|
break |
|
|
|
case "POST": |
|
|
|
err = r.ParseForm() |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered parsing form: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
} |
|
|
|
// Return updated json
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
w.Write(returnJson) |
|
|
|
} |
|
|
|
|
|
|
|
if r.FormValue("name") != "" { |
|
|
|
petData.Name = r.FormValue("name") |
|
|
|
} |
|
|
|
if r.FormValue("status") != "" { |
|
|
|
petData.Status = r.FormValue("status") |
|
|
|
} |
|
|
|
func UpdatePetHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
var ( |
|
|
|
petData models.Pet |
|
|
|
ok bool |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
_, err = database.UpdatePet(petData) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error updating pet: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
} |
|
|
|
petData, ok = getPetDataById(w, r) |
|
|
|
if !ok { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
break |
|
|
|
case "DELETE": |
|
|
|
log.Printf("Marking pet %d as deleted\n", id) |
|
|
|
err = r.ParseForm() |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered parsing form: %s\n", err.Error()) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if r.FormValue("name") != "" { |
|
|
|
petData.Name = r.FormValue("name") |
|
|
|
} |
|
|
|
if r.FormValue("status") != "" { |
|
|
|
petData.Status = r.FormValue("status") |
|
|
|
} |
|
|
|
|
|
|
|
err = database.UpdatePet(&petData) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error updating pet: %s\n", err.Error()) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
database.DeletePet(petData) |
|
|
|
} |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
notFoundJson["code"] = 200 |
|
|
|
notFoundJson["type"] = "unknown" |
|
|
|
notFoundJson["message"] = id_str |
|
|
|
returnJson, err = json.MarshalIndent(notFoundJson, "", " ") |
|
|
|
w.Write(returnJson) |
|
|
|
func DeletePetHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
var ( |
|
|
|
petData models.Pet |
|
|
|
id_str string |
|
|
|
ok bool |
|
|
|
) |
|
|
|
|
|
|
|
break |
|
|
|
petData, ok = getPetDataById(w, r) |
|
|
|
if !ok { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
log.Printf("Marking pet %d as deleted\n", petData.Id) |
|
|
|
|
|
|
|
database.DeletePet(petData) |
|
|
|
|
|
|
|
id_str = strconv.Itoa(petData.Id) |
|
|
|
genericJsonReturn(w, 200, id_str, "unknown") |
|
|
|
} |
|
|
|
|
|
|
|
func PetHandlerFindByStatus(w http.ResponseWriter, r *http.Request) { |
|
|
|
func FindByStatusPetHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
var ( |
|
|
|
status string |
|
|
|
petData []models.Pet |
|
|
@ -233,26 +265,24 @@ func PetHandlerFindByStatus(w http.ResponseWriter, r *http.Request) { |
|
|
|
) |
|
|
|
|
|
|
|
status = r.URL.Query().Get("status") |
|
|
|
if status != "available" && status != "pending" && status != "sold" { |
|
|
|
err = verifyStatus(status) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Invalid status provided to /pet/findByStatus") |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
genericJsonReturn(w, 422, "Invalid status", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
petData, err = database.GetPetsByStatus(status) |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured in GetPetsByStatus") |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(petData, "", " ") |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured while serializing pet data to JSON") |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -260,72 +290,61 @@ func PetHandlerFindByStatus(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func PetHandlerUploadImage(w http.ResponseWriter, r *http.Request) { |
|
|
|
func UploadImagePetHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
var ( |
|
|
|
urlVars map[string]string |
|
|
|
file multipart.File |
|
|
|
handler *multipart.FileHeader |
|
|
|
tempFile *os.File |
|
|
|
nameSplit []string |
|
|
|
fileBytes []byte |
|
|
|
id_str string |
|
|
|
id int |
|
|
|
ok bool |
|
|
|
err error |
|
|
|
petData models.Pet |
|
|
|
additionalMetadata string |
|
|
|
file multipart.File |
|
|
|
handler *multipart.FileHeader |
|
|
|
fileBytes []byte |
|
|
|
fileName string |
|
|
|
ok bool |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
urlVars = mux.Vars(r) |
|
|
|
id_str, ok = urlVars["petId"] |
|
|
|
petData, ok = getPetDataById(w, r) |
|
|
|
if !ok { |
|
|
|
log.Printf("Error encountered getting id\n") |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
id, err = strconv.Atoi(id_str) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered converting id to string: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
fmt.Println(id) |
|
|
|
|
|
|
|
// Upload 10Mb files
|
|
|
|
r.ParseMultipartForm(10 << 20) |
|
|
|
file, handler, err = r.FormFile("file") |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error retrieving file: %s\n", err.Error()) |
|
|
|
w.WriteHeader(http.StatusInternalServerError) |
|
|
|
w.Write([]byte("500 - An error has occured\n")) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
defer file.Close() |
|
|
|
|
|
|
|
nameSplit = strings.Split(handler.Filename, ".") |
|
|
|
|
|
|
|
tempFile, err = ioutil.TempFile("./uploads", "upload-*."+nameSplit[len(nameSplit)-1]) |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
return |
|
|
|
additionalMetadata = r.FormValue("additionalMetadata") |
|
|
|
if additionalMetadata == "" { |
|
|
|
additionalMetadata = "null" |
|
|
|
} |
|
|
|
defer tempFile.Close() |
|
|
|
|
|
|
|
fileBytes, err = ioutil.ReadAll(file) |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
log.Printf("An error occured reading POST file: %s\n", err.Error()) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
tempFile.Write(fileBytes) |
|
|
|
|
|
|
|
err = database.AddPhotoToPet(id, filepath.Base(tempFile.Name())) |
|
|
|
fileName, err = database.AddPhotoToPet(&petData, handler.Filename, additionalMetadata, fileBytes) |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
log.Printf("An error occured adding file to database: %s\n", err.Error()) |
|
|
|
genericJsonReturn(w, 500, "An error occured", "unknown") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
fmt.Fprintf(w, "Successfully Uploaded File\n") |
|
|
|
genericJsonReturn( |
|
|
|
w, |
|
|
|
200, |
|
|
|
fmt.Sprintf( |
|
|
|
"additionalMetadata: %s\nFile uploaded to %s, %d bytes", |
|
|
|
additionalMetadata, |
|
|
|
fileName, |
|
|
|
handler.Size, |
|
|
|
), |
|
|
|
"unknown", |
|
|
|
) |
|
|
|
} |