Skip to content
Snippets Groups Projects
Commit d264e4ee authored by Johannes Borgen's avatar Johannes Borgen
Browse files

merged comment mismatch

parents 92c46882 fd9dbe2c
No related branches found
No related tags found
1 merge request!4Finished app
......@@ -31,6 +31,7 @@ type FirestoreDatabase struct {
Client *firestore.Client
}
//DB is a Firebase database variable
var DB FirestoreDatabase
// Init initialised the DB.
......@@ -51,19 +52,19 @@ func (db *FirestoreDatabase) Close() {
db.Client.Close()
}
// Save saves a students into the DB.
// AddReview adds review to the database
func (db *FirestoreDatabase) AddReview(r *Review) (string, error) {
ref := db.Client.Collection(db.CollectionName).NewDoc()
r.ID = ref.ID
_, err := ref.Set(db.Ctx, r)
if err != nil {
fmt.Println("ERROR saving student to Firestore DB: ", err)
fmt.Println("ERROR saving review to Firestore DB: ", err)
return "", nil
}
return r.ID, nil
}
// GetAll gets all the routes from the database
func (db *FirestoreDatabase) GetAll() ([]Review, error) {
var reviewData Review
var request[] Review
......@@ -86,6 +87,7 @@ func (db *FirestoreDatabase) GetAll() ([]Review, error) {
return request, nil
}
// GetByID gets a route by id from the database
func (db *FirestoreDatabase) GetByID(id string) ([]Review, error){
var reviewData Review
......@@ -109,7 +111,7 @@ func (db *FirestoreDatabase) GetByID(id string) ([]Review, error){
return request, nil
}
//Delete function to delete review by id
// DeleteByTurID function to delete review by id
func (db *FirestoreDatabase) DeleteByTurID(id string) error{
docRef := db.Client.Collection("reviews").Doc(id)
......@@ -137,14 +139,14 @@ func (db *MockDatabase) Init() error { return nil }
// Close closes the DB. It does nothing.
func (db *MockDatabase) Close() {}
// Delete object by id. It does nothing.
// DeleteByTurID deletes object by id. It does nothing.
func (db *MockDatabase) DeleteByTurID(string) error { return nil}
// Adds a review. It does nothing
// AddReview Adds a review. It does nothing
func (db *MockDatabase) AddReview(*Review) (string, error) { return "", nil}
// Gets all reviews. It does nothing.
// GetAll Gets all reviews. It does nothing.
func (db *MockDatabase) GetAll() ([]Review, error) { return []Review{}, nil }
// Gets all reviews for a specific id. It does nothing.
// GetByID Gets all reviews for a specific id. It does nothing.
func (db *MockDatabase) GetByID(string) ([]Review, error) { return []Review{}, nil }
......@@ -37,6 +37,7 @@ func TestFirestoreDatabase(t *testing.T) {
if err != nil {
t.Error(err)
}
//Adding a test review
review := Review{
Navn: "Test",
......
......@@ -7,6 +7,7 @@ import (
"strings"
)
// Review is a struct for data to enter in to a JSON object
type Review struct {
Navn string `json:"navn"`
TurID string `json:"turid"`
......@@ -16,7 +17,7 @@ type Review struct {
}
// ReviewHandler handles requests to enter route reviews
func ReviewHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
......@@ -39,9 +40,10 @@ func ReviewHandler(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&reviewData)
if err != nil {
http.Error(w, "Something went wrong:" + err.Error(), http.StatusBadRequest)
return
}
if !(reviewData.TerningKast >= 1 && reviewData.TerningKast <=6 || reviewData.Beskrivelse == "" || reviewData.Navn == ""){
if (reviewData.TerningKast < 1 && reviewData.TerningKast > 6 || reviewData.Beskrivelse == "" || reviewData.Navn == ""){
http.Error(w, "Invalid payload", http.StatusBadRequest)
}else {
id, err := DB.AddReview(&reviewData)
......
......@@ -6,8 +6,10 @@ import (
"time"
)
// StartTime is the variable initialized in main to display in status.
var StartTime time.Time
// Status strut for data to enter into JSON object.
type Status struct {
Nasjonalturbase int `json:"nasjonalturbase"`
MET int `json:"metrologisk institutt"`
......@@ -16,6 +18,7 @@ type Status struct {
Version string `json:"version"`
}
// StatusHandler is the handler for the status endpoint.
func StatusHandler(w http.ResponseWriter, r *http.Request) { // Refereed to in main
//Make struct
......
......@@ -8,7 +8,7 @@ import (
)
//struct for gathering data with id in the url
// Tur struct for gathering data with id in the url
type Tur struct {
Navn string `json:"navn"`
ID string `json:"_id"`
......@@ -20,11 +20,13 @@ type Tur struct {
Forecast FinalReport `json:"forecast"`
}
// Geo object to use in array for Tur struct
type Geo struct {
Type string `json:"type"`
Coordinates [][]float64 `json:"coordinates"`
}
// TurHandler handles requests for the tur endpoint
func TurHandler (w http.ResponseWriter, r *http.Request){
switch r.Method {
......
......@@ -8,7 +8,7 @@ import (
)
//Struct for getting all the different trips
//Turer struct for getting all the different trips
type Turer struct {
Navn string `json:"navn"`
ID string `json:"_id"`
......@@ -20,7 +20,7 @@ type documents struct{
Entry []Turer `json:"documents"`
}
//Gets the data of one trip and a list of all the trips
// TurerHandler Gets the data of one trip and a list of all the trips
func TurerHandler (w http.ResponseWriter, r *http.Request){
switch r.Method {
......
......@@ -13,14 +13,14 @@ import (
// we use the locationforecast v1.9 module and specify json format instead of xml
const metAPI = "https://api.met.no/weatherapi/locationforecast/1.9/.json"
// Parent struct for response parsing
// SingleForecast Parent struct for response parsing
type singleForecast struct {
Product struct{
ResultData []ForecastData `json:"time"`
} `json:"product"`
}
// Struct for relevant temperature and rain data from Metrologisk institutt
// ForecastData Struct for relevant temperature and rain data from Metrologisk institutt
type ForecastData struct {
From time.Time `json:"from"`
To time.Time `json:"to"`
......@@ -41,12 +41,14 @@ type ForecastData struct {
} `json:"location"`
}
// FinalReport struct for formatting the data of the final report in the JSON response
type FinalReport struct {
ForecastMap map[string]string `json:"temperatures"`
RainMap map[string]string `json:"rain"`
License string `json:"license"`
}
// WeatherHandler handles the tur and turers requests for weather data.
func WeatherHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
......@@ -89,7 +91,7 @@ func GetFloat(query string) (float64, error) {
return 0, err
}
// Parses the response from Metrologisk institutt,
// ParseWeatherData Parses the response from Metrologisk institutt,
func ParseWeatherData(data []ForecastData) (FinalReport, error) {
var result = FinalReport{}
result.ForecastMap = make(map[string]string)
......@@ -124,7 +126,7 @@ func ParseWeatherData(data []ForecastData) (FinalReport, error) {
return result, nil
}
// For a langitude and latitude value, retrieve temperature and rain forecast
// GetSingleForecast gets a sinlgle forecast For a langitude and latitude value, retrieve temperature and rain forecast
func GetSingleForecast(lat, long float64) (FinalReport, error) {
url := metAPI + fmt.Sprintf("?lat=%f&lon=%f", lat, long)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment