Commit 1601a469 authored by Enrico Bollen's avatar Enrico Bollen 💬
Browse files

get roomByID

parent beddf267
Pipeline #1801 failed with stages
in 56 seconds
......@@ -19,8 +19,8 @@ func CreateRoom(buildingId int, roomNo int, capacity int) *m.Room {
}
// GetRoom gets a single room by id
func GetRoom(buildingId int, roomNo int) (room *m.Room, err error) {
room, err = s.BuildingService.GetRoom(buildingId, roomNo)
func GetRoom(roomId int) (room *m.Room, err error) {
room, err = s.BuildingService.GetRoom(roomId)
return room, err
}
......
......@@ -9,6 +9,7 @@ import (
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)
type roomRouter struct{}
......@@ -22,9 +23,9 @@ func createRoom(res http.ResponseWriter, req *http.Request) {
// Decode the body
type incoming struct {
Building int
Number int
Capacity int
Building int `json:"building"`
Number int `json:"number"`
Capacity int `json:"capacity"`
}
var r incoming
err := json.NewDecoder(req.Body).Decode(&incoming{})
......@@ -32,7 +33,6 @@ func createRoom(res http.ResponseWriter, req *http.Request) {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
room := c.CreateRoom(r.Building, r.Number, r.Capacity)
err = u.SendJSON(res).Encode(room)
......@@ -42,28 +42,21 @@ func createRoom(res http.ResponseWriter, req *http.Request) {
}
func getRoom(res http.ResponseWriter, req *http.Request) {
var roomId int
// Decode the body
type incoming struct {
Building int
Number int
}
var r incoming
err := json.NewDecoder(req.Body).Decode(&incoming{})
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
roomId, parseErr := strconv.Atoi(mux.Vars(req)["roomId"])
room, err := s.BuildingService.GetRoom(r.Building, r.Number)
if parseErr == nil {
if err == nil {
err := u.SendJSON(res).Encode(room)
if err != nil {
fmt.Println(err)
room, roomErr := c.GetRoom(roomId)
if roomErr == nil {
u.SendJSON(res).Encode(room)
} else {
http.Error(res, roomErr.Error(), http.StatusNotFound)
}
} else {
http.Error(res, err.Error(), http.StatusNotFound)
http.Error(res, parseErr.Error(), http.StatusBadRequest)
}
}
......
......@@ -35,9 +35,17 @@ func (b *Building) New(openFrom string, openDuration string, workdays []string)
// AddRoom adds rooms to building.
// takes number and capacity
func (b *Building) AddRoom(roomNumber int, capacity int) (room *Room, err error) {
all := b.GetRooms()
var onlyRooms []*Room
for _, rooms := range all {
onlyRooms = append(onlyRooms, rooms)
}
id := len(all)
room, err = b.GetRoomByNo(roomNumber)
if err != nil {
room = &Room{Number: roomNumber, Capacity: capacity}
room = &Room{ID: id, Number: roomNumber, Capacity: capacity}
b.Rooms = append(b.Rooms, room)
err = nil
}
......
......@@ -5,15 +5,14 @@ import (
"sort"
"git.coco.study/fvitt/good2go/internal/utils"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// Room Room Entity
type Room struct {
ID primitive.ObjectID `bson:"_id"`
Number int `bson:"number"`
Capacity int `bson:"capacity"`
Appointments []Appointment `bson:"appointments"`
ID int `bson:"_id"`
Number int `bson:"number"`
Capacity int `bson:"capacity"`
Appointments []Appointment `bson:"appointments"`
}
// AddAppointment adds an appointment to the room
......
......@@ -49,12 +49,20 @@ func (b *buildingService) AddRoom(buildingId int, roomNumber int, capacity int)
return room, err
}
func (b *buildingService) GetRoom(buildingId int, roomNumber int) (room *model.Room, err error) {
building, err := b.GetBuilding(buildingId)
if err != nil {
fmt.Println(err)
} else {
room, err = building.GetRoomByNo(roomNumber)
func (b *buildingService) GetRoom(roomId int) (room *model.Room, err error) {
rooms := b.GetRooms()
for index, _ := range rooms {
for _, curroom := range rooms[index] {
if curroom.ID == roomId {
room = curroom
err = nil
return room, err
}
}
}
if room == nil {
err = errors.New("room not found.")
}
return room, err
}
......
......@@ -5,6 +5,7 @@ import (
)
func TestBuildingService_AddRoom(t *testing.T) {
BuildDefBuilding()
room, err := BuildingService.AddRoom(0, 250, 50)
if err != nil {
t.Error(err)
......@@ -29,7 +30,7 @@ func TestBuildingService_GetRoom(t *testing.T) {
BuildDefBuilding()
// try to get an actual room
room, err := BuildingService.GetRoom(0, 200)
room, err := BuildingService.GetRoom(0)
if err != nil {
t.Error(err)
}
......@@ -39,8 +40,8 @@ func TestBuildingService_GetRoom(t *testing.T) {
err = nil
room = nil
// pass wrong building id
room, err = BuildingService.GetRoom(1, 200)
// pass wrong room id
room, err = BuildingService.GetRoom(1)
if err == nil {
t.Error("error expected, got: ", err)
}
......@@ -86,7 +87,7 @@ func TestBuildingService_DeleteRoom(t *testing.T) {
func TestBuildingService_UpdateRoomCapacity(t *testing.T) {
BuildDefBuilding()
room, err := BuildingService.UpdateRoomCapacity(0, 200, 20)
room2, err := BuildingService.GetRoom(0, 200)
room2, err := BuildingService.GetRoom(0)
if err != nil {
t.Error(err)
}
......
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