Commit 8d7b2b1a authored by Enrico Bollen's avatar Enrico Bollen 💬
Browse files

fixes code style issues

parent 72ae79e0
Pipeline #1783 passed with stages
in 2 minutes and 55 seconds
......@@ -15,8 +15,6 @@ coverage:
build:
go build -o . ./cmd/main.go
clean:
# Need to install reflex
# go get github.com/cespare/reflex
watch:
......
# Good2Go – Roombooking
# Good2Go – RoomBooking
[Error Tracking](https://git.coco.study/fvitt/good2go/-/error_tracking)
[Coverage Report](https://fvitt.pages.coco.study/good2go/master/coverage)
......
......@@ -10,7 +10,7 @@ RestAPI Module
## Components
The api module is seperated into three major components
The api module is separated into three major components
### Router
......
package controllers
import "time"
/*
// CreateAppointment creates an appointment.
func CreateAppointment() {
......@@ -26,3 +25,5 @@ func UpdateAppointment() {
func DeleteAppointment() {
}
*/
package controllers
/*
// CreateBuilding Creates a single new building
func CreateBuilding() {
// Reservered for future use
// Reserved for future use
}
// GetBuilding gets a single building
func GetBuilding() {
// Reservered for future use
// Reserved for future use
}
// GetAllBuildings Returns all the buildings
func GetAllBuildings() {
// Reservered for future use
// Reserved for future use
}
// UpdateBuilding Updates a single building
func UpdateBuilding() {
// Reservered for future use
// Reserved for future use
}
// DeleteBuilding Deletes a single building
func DeleteBuilding(id int) {
// Reservered for future use
// Reserved for future use
}
*/
......@@ -81,9 +81,10 @@ func TestUpdateSingleRoom(t *testing.T) {
if err != nil {
t.Errorf("Could not find created room")
}
if room.Capacity != 90 {
t.Error("Updated room didnt have the correct capacity")
if room != nil {
if room.Capacity != 90 {
t.Error("Updated room didnt have the correct capacity")
}
}
}
......
......@@ -26,7 +26,7 @@ func Init() {
// INIT ALL THE THINGS
r := mux.NewRouter()
// Add all the middlewares
// Add all the middleware
r.Use(m.LogRequests)
r.Use(m.SentryHandler)
......@@ -38,7 +38,7 @@ func Init() {
// Create a new server
srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:" + getPort(),
Addr: "0.0.0.0:8080",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
......
......@@ -32,7 +32,7 @@ func deleteAppointment(res http.ResponseWriter, req *http.Request) {
}
// AttachRouter Creates and attaches a subrouter to the given routers
// AttachRouter Creates and attaches a sub-router to the given routers
func (a appointmentRouter) AttachRouter(router *mux.Router) {
r := router.PathPrefix("/appointments").Subrouter()
......
......@@ -2,6 +2,7 @@ package restapi
import (
"encoding/json"
"fmt"
"html"
"net/http"
......@@ -20,11 +21,17 @@ var (
var name = "Home"
func createHome(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Why would you want to create a new /? This one is perfectly fine!"))
_, err := w.Write([]byte("Why would you want to create a new /? This one is perfectly fine!"))
if err != nil {
fmt.Println(err)
}
}
func getHome(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from " + name))
_, err := w.Write([]byte("Hello from " + name))
if err != nil {
fmt.Println(err)
}
}
func min(a, b int) int {
......@@ -58,11 +65,17 @@ func updateHome(res http.ResponseWriter, req *http.Request) {
name = limitString(html.EscapeString(r.name), 20)
res.Write([]byte("Updated home: new name " + name + "!?!?"))
_, err := res.Write([]byte("Updated home: new name " + name + "!?!?"))
if err != nil {
fmt.Println(err)
}
}
func deleteHome(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("What are you thinking, you cant delete /, are you crazy!?!?"))
_, err := w.Write([]byte("What are you thinking, you cant delete /, are you crazy!?!?"))
if err != nil {
fmt.Println(err)
}
}
func (a homeRouter) AttachRouter(r *mux.Router) {
......
......@@ -2,12 +2,13 @@ package restapi
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
c "git.coco.study/fvitt/good2go/api/rest/controllers"
u "git.coco.study/fvitt/good2go/api/rest/utils"
model "git.coco.study/fvitt/good2go/internal/model"
"git.coco.study/fvitt/good2go/internal/model"
"github.com/gorilla/mux"
)
......@@ -30,7 +31,10 @@ func createRoom(res http.ResponseWriter, req *http.Request) {
room := c.CreateRoom(r.Number, r.Capacity)
u.SendJSON(res).Encode(room)
err = u.SendJSON(res).Encode(room)
if err != nil {
fmt.Println(err)
}
}
func getRoom(res http.ResponseWriter, req *http.Request) {
......@@ -44,7 +48,10 @@ func getRoom(res http.ResponseWriter, req *http.Request) {
room, roomErr := c.GetRoom(roomNo)
if roomErr == nil {
u.SendJSON(res).Encode(room)
err := u.SendJSON(res).Encode(room)
if err != nil {
fmt.Println(err)
}
} else {
http.Error(res, roomErr.Error(), http.StatusNotFound)
}
......@@ -57,8 +64,10 @@ func getAllRooms(res http.ResponseWriter, req *http.Request) {
rooms := c.GetAllRooms()
u.SendJSON(res).Encode(rooms)
err := u.SendJSON(res).Encode(rooms)
if err != nil {
fmt.Println(err)
}
}
func updateRoom(res http.ResponseWriter, req *http.Request) {
......
package mongo
import (
"fmt"
"time"
repos "git.coco.study/fvitt/good2go/database/mongo/repositories"
utils "git.coco.study/fvitt/good2go/database/mongo/utils"
"git.coco.study/fvitt/good2go/database/mongo/utils"
)
var (
......@@ -18,7 +19,12 @@ func Init() {
// Create a mongodb client
client, ctx := utils.CreateDBClient(10 * time.Second)
defer client.Disconnect(ctx)
defer func() {
err := client.Disconnect(ctx)
if err != nil {
fmt.Println(err)
}
}()
// Connect to a specified database
db := client.Database(utils.GetDBName())
......
......@@ -2,11 +2,12 @@ package mongo
import (
"errors"
"fmt"
"log"
"strconv"
"time"
utils "git.coco.study/fvitt/good2go/database/mongo/utils"
"git.coco.study/fvitt/good2go/database/mongo/utils"
"git.coco.study/fvitt/good2go/internal/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
......@@ -80,7 +81,10 @@ func (repo *RoomRepository) updateOne(document interface{}) {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
repo.Col.UpdateOne(ctx, document, document)
_, err := repo.Col.UpdateOne(ctx, document, document)
if err != nil {
fmt.Println(err)
}
}
func (repo *RoomRepository) findOne(document interface{}) (*model.Room, error) {
......@@ -99,9 +103,9 @@ func (repo *RoomRepository) deleteByID(id primitive.ObjectID) error {
ctx, cancel := utils.CreateTimeoutCTX(5 * time.Second)
defer cancel()
_, error := repo.Col.DeleteOne(ctx, bson.M{"_id": id})
_, err := repo.Col.DeleteOne(ctx, bson.M{"_id": id})
return error
return err
}
func (repo *RoomRepository) CreateRoom(r *model.Room) (*model.Room, error) {
......
package mongo
import (
"fmt"
"testing"
"git.coco.study/fvitt/good2go/internal/model"
......@@ -19,10 +20,10 @@ func TestMain(m *testing.M) {
func Test_roomRepository_CreateRoom(t *testing.T) {
repo.CreateRoom(&model.Room{
Number: 100,
Capacity: 10,
})
_, err := repo.CreateRoom(&model.Room{Number: 100, Capacity: 10})
if err != nil {
fmt.Println(err)
}
room, err := repo.findByRoomNumber(100)
if err != nil {
......@@ -55,12 +56,12 @@ func Test_roomRepository_UpdateRoom(t *testing.T) {
func Test_roomRepository_GetRoom(t *testing.T) {
repo.CreateRoom(&model.Room{
Number: 420,
Capacity: 22,
})
_, err := repo.GetRoom(bson.M{
_, err := repo.CreateRoom(&model.Room{Number: 420, Capacity: 22})
if err != nil {
fmt.Println(err)
panic(err)
}
_, err = repo.GetRoom(bson.M{
"number": 420,
})
......
......@@ -2,16 +2,19 @@ package utils
import (
"flag"
"fmt"
"os"
godotenv "github.com/joho/godotenv"
"github.com/joho/godotenv"
)
// GetEnv gets a env variable, and when not present returns the default value
func GetEnv(variableName string, defaultValue string) string {
godotenv.Load(".env")
err := godotenv.Load(".env")
if err != nil {
fmt.Println(err)
}
envValue := os.Getenv(variableName)
if envValue == "" {
......
......@@ -44,7 +44,7 @@ func (b *building) AddRoom(roomNumber int, capacity int) (room Room) {
}
// DeleteRoom deletes a room from building.
// pass roomnumber
// pass roomNumber
func (b *building) DeleteRoom(roomNumber int) {
for index, room := range b.Rooms {
if room.Number == roomNumber {
......@@ -74,7 +74,7 @@ func (b *building) GetRoomByNo(roomNumber int) (room *Room, err error) {
return room, err
}
}
err = errors.New("Room not found")
err = errors.New("room not found.")
return room, err
}
......@@ -108,7 +108,7 @@ func (b *building) FindAvailableRooms(reqAppoint Appointment) (selectedRooms []*
if len(selectedRooms) <= 0 {
// no rooms found
err = errors.New("No room found for date")
err = errors.New("no room found for date.")
} else {
// sort after capacity … lowest on index 0
sort.Slice(selectedRooms, func(i, j int) bool {
......
......@@ -112,7 +112,7 @@ func TestBuilding_FindFreeRooms(t *testing.T) {
requestedAppoint, _ := Appointment{}.New("30-05-2020 10:00", "2h")
rooms, err := Building.FindAvailableRooms(requestedAppoint)
if err != nil {
t.Error(err, "\n Exptected: \n", Building.Rooms[0], "\ngot:\n", rooms)
t.Error(err, "\n Expected: \n", Building.Rooms[0], "\ngot:\n", rooms)
}
// Should result in error since there is already an appointment on that date
......
......@@ -27,7 +27,7 @@ func (r *Room) AddAppointment(appointment Appointment) {
}
// DeleteAppointment deletes an appointment on room level.
// Takes just the startdate, since their can only be one Appointment with this date.
// Takes just the startDate, since their can only be one Appointment with this date.
func (r *Room) DeleteAppointment(startDate string) {
for index, appoint := range r.Appointments {
if appoint.StartDate.Equal(utils.FormatTime(startDate)) {
......@@ -44,7 +44,7 @@ func (r *Room) GetAppointment(startDate string) (appointment *Appointment, err e
}
}
if appointment == nil {
err = errors.New("No appointment found")
err = errors.New("no appointment found.")
}
return appointment, err
}
......@@ -41,11 +41,11 @@ func (a *appointmentPlanner) GetAppointments() (appointmentsMap map[int][]model.
}
// GetAppointment gets a specific appointment in a room, returns map where roomNumber is key
func (a *appointmentPlanner) GetAppointment(roomNumber int, startdate string) (appointmentMap map[int]*model.Appointment, err error) {
func (a *appointmentPlanner) GetAppointment(roomNumber int, startDate string) (appointmentMap map[int]*model.Appointment, err error) {
appointmentMap = make(map[int]*model.Appointment)
room, err := model.Building.GetRoomByNo(roomNumber)
if err == nil {
appointmentMap[roomNumber], err = room.GetAppointment(startdate)
appointmentMap[roomNumber], err = room.GetAppointment(startDate)
}
return appointmentMap, err
}
......@@ -8,21 +8,21 @@ import (
// Go Lang reference Time:
// Mon Jan 2 15:04:05 MST 2006
func FormatTime(inputTime string) time.Time {
const timelayout string = "02-01-2006 15:04"
formattedTime, _ := time.Parse(timelayout, inputTime)
const timeLayout string = "02-01-2006 15:04"
formattedTime, _ := time.Parse(timeLayout, inputTime)
return formattedTime
}
func FormatHour(input string) time.Time {
const timelayout string = "15:04"
formattedTime, _ := time.Parse(timelayout, input)
const timeLayout string = "15:04"
formattedTime, _ := time.Parse(timeLayout, input)
return formattedTime
}
func FormatDay(input string) time.Weekday {
input = strings.ToLower(input)
//const timelayout string = "Mon"
//const timeLayout string = "Mon"
switch input {
case "mon":
return time.Monday
......@@ -41,6 +41,6 @@ func FormatDay(input string) time.Weekday {
default:
return time.Weekday(0)
}
//formattedTime, _ := time.Parse(timelayout, input)
//formattedTime, _ := time.Parse(timeLayout, input)
//return formattedTime.Weekday()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment