Skip to content

Commit

Permalink
Merge branch 'master' of github.com:anGie44/animal-rescue
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Mar 19, 2020
2 parents e76705a + b4d315f commit 8d934fb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 9 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

To run the server on your system:

1. Create a `.env` file with your Auth0 Credentials (only includes vars referenced in `main.go`) [Sign up](https://auth0.com) for an account for free if you don't have one.
2. Update the `auth0-variables.js` file in `static/js` with your Auth0 Credentials.
3. Add `http://localhost:3000` to your Allowed Callback, and Allowed Logout URL's in your [Auth0 Management Dashboard](https://manage.auth0.com)
4. Make sure you have [dep](https://github.com/golang/dep) installed
5. Run `dep ensure` to install dependencies
6. Run `go build` to create the binary (`animal-rescue`)
7. Run the binary : `./animal-rescue`
1. Make sure you have [dep](https://github.com/golang/dep) installed
2. Run `dep ensure` to install dependencies
3. Run `go build` to create the binary (`animal-rescue`)
4. Run the binary : `./animal-rescue`

To run tests:

Expand Down
4 changes: 2 additions & 2 deletions adoptee_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var createAdopteeHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.
adoptee.Age = r.Form.Get("age")

adoptees = append(adoptees, &adoptee)

http.Redirect(w, r, "/assets/", http.StatusFound)
payload, _ := json.Marshal(adoptees)
w.Write([]byte(payload))

})

Expand Down
121 changes: 121 additions & 0 deletions adoption_handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/gorilla/mux"
)

type Adoption struct {
ID int `json:"id"`
Adopter *Adopter `json:"adopter"`
Adoptee *Adoptee `json:"adoptee"`
Date string `json:"date"`
}

var adoptions []*Adoption

var createAdoptionHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
adopterID, err := strconv.Atoi(vars["adopter_id"])
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
adopteeID, err := strconv.Atoi(vars["adoptee_id"])
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}

adopter, _ := getAdopterByID(adopterID)
adoptee, _ := getAdopteeByID(adopteeID)
if adopter == nil || adoptee == nil {
w.Write([]byte("Adopter ID and/or Adoptee ID not found"))
return
}
adoption := Adoption{}
adoption.ID = len(adoptions) + 1
adoption.Adopter = adopter
adoption.Adoptee = adoptee
adoptions = append(adoptions, &adoption)

payload, _ := json.Marshal(adoptions)
w.Write([]byte(payload))

})

var getAdoptionHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])

if err != nil {
w.Write([]byte("Adoption ID not valid"))
return
}

w.Header().Set("Content-Type", "application/json")
adoption, _ := getAdoptionByID(id)
if adoption != nil {
payload, _ := json.Marshal(adoption)
w.Write([]byte(payload))
} else {
w.Write([]byte("Adoption Not Found"))
}

})

var getAdoptionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
adoptionListBytes, err := json.Marshal(adoptions)
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(adoptionListBytes)
})

var deleteAdoptionHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// GET adopter and remove from list stored in memory
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])

if err != nil {
w.Write([]byte("Adoption ID not valid"))
return
}

adoption, index := getAdoptionByID(id)
if adoption != nil {
removeAdoptionByID(index)
w.Write([]byte("Adoption with ID " + string(index) + " removed"))
} else {
w.Write([]byte("Adoption Not Found"))
}
})

// Helper Functions

func getAdoptionByID(id int) (*Adoption, int) {
var adoption *Adoption
var index int
for i, a := range adoptions {
if a.ID == id {
adoption = a
index = i
}
}
return adoption, index
}

func removeAdoptionByID(index int) {
var emptyAdoption *Adoption
adoptions[index] = adoptions[len(adoptions)-1]
adoptions[len(adoptions)-1] = emptyAdoption
adoptions = adoptions[:len(adoptions)-1]
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func main() {
r.Handle("/petpref/{id}/update", updatePetPrefenceHandler).Methods("GET")
r.Handle("/petpref/{id}/delete", deletePetPreferenceHandler).Methods("GET")

r.Handle("/adoption/adopter/{adopter_id}/adoptee/{adoptee_id}", createAdoptionHandler).Methods("GET")
r.Handle("/adoptions", getAdoptionsHandler).Methods("GET")
r.Handle("/adoption/{id}", getAdoptionHandler).Methods("GET")

http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, r))

}
Expand Down

0 comments on commit 8d934fb

Please sign in to comment.