Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Fabian Vitt
good2go
Commits
8d7b2b1a
Commit
8d7b2b1a
authored
Jun 03, 2020
by
Enrico Bollen
💬
Browse files
fixes code style issues
parent
72ae79e0
Pipeline
#1783
passed with stages
in 2 minutes and 55 seconds
Changes
19
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
8d7b2b1a
...
...
@@ -15,8 +15,6 @@ coverage:
build
:
go build
-o
.
./cmd/main.go
clean
:
# Need to install reflex
# go get github.com/cespare/reflex
watch
:
...
...
README.md
View file @
8d7b2b1a
# Good2Go – Room
b
ooking
# Good2Go – Room
B
ooking
[
Error Tracking
](
https://git.coco.study/fvitt/good2go/-/error_tracking
)
[
Coverage Report
](
https://fvitt.pages.coco.study/good2go/master/coverage
)
...
...
api/rest/README.md
View file @
8d7b2b1a
...
...
@@ -10,7 +10,7 @@ RestAPI Module
## Components
The api module is sep
e
rated into three major components
The api module is sep
a
rated into three major components
### Router
...
...
api/rest/controllers/appointment_controller.go
View file @
8d7b2b1a
package
controllers
import
"time"
/*
// CreateAppointment creates an appointment.
func CreateAppointment() {
...
...
@@ -26,3 +25,5 @@ func UpdateAppointment() {
func DeleteAppointment() {
}
*/
api/rest/controllers/building_controller.go
View file @
8d7b2b1a
package
controllers
/*
// CreateBuilding Creates a single new building
func CreateBuilding() {
// Reserve
re
d for future use
// Reserved for future use
}
// GetBuilding gets a single building
func GetBuilding() {
// Reserve
re
d for future use
// Reserved for future use
}
// GetAllBuildings Returns all the buildings
func GetAllBuildings() {
// Reserve
re
d for future use
// Reserved for future use
}
// UpdateBuilding Updates a single building
func UpdateBuilding() {
// Reserve
re
d for future use
// Reserved for future use
}
// DeleteBuilding Deletes a single building
func DeleteBuilding(id int) {
// Reserve
re
d for future use
// Reserved for future use
}
*/
api/rest/controllers/room_controller_test.go
View file @
8d7b2b1a
...
...
@@ -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"
)
}
}
}
...
...
api/rest/router.go
View file @
8d7b2b1a
...
...
@@ -26,7 +26,7 @@ func Init() {
// INIT ALL THE THINGS
r
:=
mux
.
NewRouter
()
// Add all the middleware
s
// 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
,
...
...
api/rest/subrouters/appointment_router.go
View file @
8d7b2b1a
...
...
@@ -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
()
...
...
api/rest/subrouters/home_router.go
View file @
8d7b2b1a
...
...
@@ -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
)
{
...
...
api/rest/subrouters/room_router.go
View file @
8d7b2b1a
...
...
@@ -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
)
{
...
...
database/mongo/main.go
View file @
8d7b2b1a
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
())
...
...
database/mongo/repositories/room_repository.go
View file @
8d7b2b1a
...
...
@@ -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
()
_
,
err
or
:=
repo
.
Col
.
DeleteOne
(
ctx
,
bson
.
M
{
"_id"
:
id
})
_
,
err
:=
repo
.
Col
.
DeleteOne
(
ctx
,
bson
.
M
{
"_id"
:
id
})
return
err
or
return
err
}
func
(
repo
*
RoomRepository
)
CreateRoom
(
r
*
model
.
Room
)
(
*
model
.
Room
,
error
)
{
...
...
database/mongo/repositories/room_repository_test.go
View file @
8d7b2b1a
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
,
})
...
...
database/mongo/utils/env.go
View file @
8d7b2b1a
...
...
@@ -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
==
""
{
...
...
internal/model/building.go
View file @
8d7b2b1a
...
...
@@ -44,7 +44,7 @@ func (b *building) AddRoom(roomNumber int, capacity int) (room Room) {
}
// DeleteRoom deletes a room from building.
// pass room
n
umber
// pass room
N
umber
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
(
"
R
oom not found"
)
err
=
errors
.
New
(
"
r
oom 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
(
"
N
o room found for date"
)
err
=
errors
.
New
(
"
n
o room found for date
.
"
)
}
else
{
// sort after capacity … lowest on index 0
sort
.
Slice
(
selectedRooms
,
func
(
i
,
j
int
)
bool
{
...
...
internal/model/building_test.go
View file @
8d7b2b1a
...
...
@@ -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
Exp
t
ected:
\n
"
,
Building
.
Rooms
[
0
],
"
\n
got:
\n
"
,
rooms
)
t
.
Error
(
err
,
"
\n
Expected:
\n
"
,
Building
.
Rooms
[
0
],
"
\n
got:
\n
"
,
rooms
)
}
// Should result in error since there is already an appointment on that date
...
...
internal/model/room.go
View file @
8d7b2b1a
...
...
@@ -27,7 +27,7 @@ func (r *Room) AddAppointment(appointment Appointment) {
}
// DeleteAppointment deletes an appointment on room level.
// Takes just the start
d
ate, since their can only be one Appointment with this date.
// Takes just the start
D
ate, 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
(
"
N
o appointment found"
)
err
=
errors
.
New
(
"
n
o appointment found
.
"
)
}
return
appointment
,
err
}
internal/services/appointment_planner.go
View file @
8d7b2b1a
...
...
@@ -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
,
start
d
ate
string
)
(
appointmentMap
map
[
int
]
*
model
.
Appointment
,
err
error
)
{
func
(
a
*
appointmentPlanner
)
GetAppointment
(
roomNumber
int
,
start
D
ate
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
(
start
d
ate
)
appointmentMap
[
roomNumber
],
err
=
room
.
GetAppointment
(
start
D
ate
)
}
return
appointmentMap
,
err
}
internal/utils/timehelper.go
View file @
8d7b2b1a
...
...
@@ -8,21 +8,21 @@ import (
// Go Lang reference Time:
// Mon Jan 2 15:04:05 MST 2006
func
FormatTime
(
inputTime
string
)
time
.
Time
{
const
time
l
ayout
string
=
"02-01-2006 15:04"
formattedTime
,
_
:=
time
.
Parse
(
time
l
ayout
,
inputTime
)
const
time
L
ayout
string
=
"02-01-2006 15:04"
formattedTime
,
_
:=
time
.
Parse
(
time
L
ayout
,
inputTime
)
return
formattedTime
}
func
FormatHour
(
input
string
)
time
.
Time
{
const
time
l
ayout
string
=
"15:04"
formattedTime
,
_
:=
time
.
Parse
(
time
l
ayout
,
input
)
const
time
L
ayout
string
=
"15:04"
formattedTime
,
_
:=
time
.
Parse
(
time
L
ayout
,
input
)
return
formattedTime
}
func
FormatDay
(
input
string
)
time
.
Weekday
{
input
=
strings
.
ToLower
(
input
)
//const time
l
ayout string = "Mon"
//const time
L
ayout 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(time
l
ayout, input)
//formattedTime, _ := time.Parse(time
L
ayout, input)
//return formattedTime.Weekday()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment