You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
8.8 KiB
Go
334 lines
8.8 KiB
Go
package main
|
|
|
|
import (
|
|
"digital_temperature_monitor/models"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// @Summary Get all the rooms added to DTM
|
|
// @Description Lists all the rooms added to DTM
|
|
// @Tags Rooms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} []models.Room
|
|
// @Router /rooms/rooms [get]
|
|
func GetRooms(c *gin.Context) {
|
|
var rooms []models.Room
|
|
DB.Find(&rooms)
|
|
c.JSON(http.StatusOK, rooms)
|
|
}
|
|
|
|
// @Summary Get a room by its name
|
|
// @Description Get a room by its name
|
|
// @Tags Rooms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param name path string true "The room to get based on its name"
|
|
// @Success 200 {object} models.Room
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 404 {object} models.Error
|
|
// @Router /rooms/get_room_by_name/{name} [get]
|
|
func GetRoomByName(c *gin.Context) {
|
|
var room models.Room
|
|
name := c.Param("name")
|
|
|
|
if err := DB.Where("name = ?", name).First(&room).Error; err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": "record not found",
|
|
})
|
|
return
|
|
} else if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error,
|
|
})
|
|
}
|
|
|
|
c.JSON(http.StatusOK, room)
|
|
}
|
|
|
|
// @Summary Get the ID for a room based on its name
|
|
// @Description Get the ID for a room based on its name
|
|
// @Tags Rooms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param name path string true "The name of the room to get the ID for"
|
|
// @Success 200 {object} int
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 404 {object} models.Error
|
|
// @Router /rooms/get_room_id_by_name/{name} [get]
|
|
func GetRoomIDByName(c *gin.Context) {
|
|
var room models.Room
|
|
name := c.Param("name")
|
|
|
|
if err := DB.Where("name = ?", name).First(&room).Error; err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"Status": "failed",
|
|
"error": "record not found",
|
|
})
|
|
return
|
|
} else if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"Status": "failed",
|
|
"Error": err.Error,
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, room.ID)
|
|
}
|
|
|
|
// AddRoom godoc
|
|
// @Summary Create a room
|
|
// @Description Create a room
|
|
// @Tags Rooms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param room body models.AddRoom true "The room to create"
|
|
// @Success 201 {object} models.AddRoom
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 409 {object} models.Error
|
|
// @Router /rooms/add_room [post]
|
|
func AddRoom(c *gin.Context) {
|
|
var input models.AddRoom
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
room := models.Room{Name: input.Name}
|
|
result := DB.Create(&room)
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"status": "failure",
|
|
"error": "room already exists in dtm",
|
|
})
|
|
return
|
|
} else if result.Error != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "failure",
|
|
"error": result.Error,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, input)
|
|
}
|
|
|
|
// DeleteRoom godoc
|
|
// @Summary Delete a room
|
|
// @Description Delete a room
|
|
// @Tags Rooms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param room_id path string true "The ID of the room to be deleted"
|
|
// @Success 204
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 404 {object} models.Error
|
|
// @Router /rooms/delete_room/{room_id} [delete]
|
|
func DeleteRoom(c *gin.Context) {
|
|
var room models.Room
|
|
roomID := c.Param("room_id")
|
|
if err := DB.Where("ID = ?", roomID).Delete(&room).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error,
|
|
})
|
|
return
|
|
} else if DB.RowsAffected < 1 {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"status": "error",
|
|
"error": "record not found",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// DeleteRoomByName godoc
|
|
// @Summary Delete a room using its name
|
|
// @Description Delete a room using it name
|
|
// @Tags Rooms
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param name path string true "The name of the room to be deleted"
|
|
// @Success 204
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 404 {object} models.Error
|
|
// @Router /rooms/delete_room_by_name/{name} [delete]
|
|
func DeleteRoomByName(c *gin.Context) {
|
|
name := c.Param("name")
|
|
var room models.Room
|
|
if result := DB.Where("name = ?", name).Unscoped().Delete(&room); result.Error != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": result.Error,
|
|
})
|
|
return
|
|
} else if result.RowsAffected < 1 {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"status": "error",
|
|
"error": "record not found",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// @Summary Get all temperatures in DTM
|
|
// @Description Lists all the temperatures in DTM for every room
|
|
// @Tags Temperatures
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} []models.Temperature
|
|
// @Router /temperatures/temperatures [get]
|
|
func GetTemperatures(c *gin.Context) {
|
|
var temperatures []models.Temperature
|
|
DB.Find(&temperatures)
|
|
c.JSON(http.StatusOK, temperatures)
|
|
}
|
|
|
|
// AddTemperature godoc
|
|
// @Summary Add a temperature
|
|
// @Description Add a temperature
|
|
// @Tags Temperatures
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param temperature body models.AddTemperature true "The temperature to create"
|
|
// @Success 201 {object} models.AddTemperature
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 404 {object} models.Error
|
|
// @Failure 409 {object} models.Error
|
|
// @Router /temperatures/add_temperature [post]
|
|
func AddTemperature(c *gin.Context) {
|
|
var addTemperature models.AddTemperature
|
|
if err := c.ShouldBindJSON(&addTemperature); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error,
|
|
})
|
|
return
|
|
}
|
|
|
|
var temperature models.Temperature
|
|
temperature.Temperature = addTemperature.Temperature
|
|
|
|
var room models.Room
|
|
name := addTemperature.RoomName
|
|
if err := DB.Where("name = ?", name).First(&room).Error; err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": "record not found",
|
|
})
|
|
return
|
|
} else if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error,
|
|
})
|
|
}
|
|
|
|
temperature.RoomID = room.ID
|
|
|
|
result := DB.Create(&temperature)
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"status": "failure",
|
|
"error": "room already exists in dtm",
|
|
})
|
|
return
|
|
} else if result.Error != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "failure",
|
|
"error": result.Error,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, temperature)
|
|
}
|
|
|
|
// @Summary Get all humidities in DTM
|
|
// @Description Lists all the humidities in DTM for every room
|
|
// @Tags Humidity
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} []models.Humidity
|
|
// @Router /humidity/humidities [get]
|
|
func GetHumidities(c *gin.Context) {
|
|
var humidities []models.Humidity
|
|
DB.Find(&humidities)
|
|
c.JSON(http.StatusOK, humidities)
|
|
}
|
|
|
|
// AddHumidity godoc
|
|
// @Summary Add a humidity
|
|
// @Description Add a humidity
|
|
// @Tags Humidity
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param humidity body models.AddHumidity true "The humidity to create"
|
|
// @Success 201 {object} models.Humidity
|
|
// @Failure 400 {object} models.Error
|
|
// @Failure 404 {object} models.Error
|
|
// @Failure 409 {object} models.Error
|
|
// @Router /humidity/add_humidity [post]
|
|
func AddHumidity(c *gin.Context) {
|
|
var addHumidity models.AddHumidity
|
|
if err := c.ShouldBindJSON(&addHumidity); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error,
|
|
})
|
|
return
|
|
}
|
|
|
|
var humidity models.Humidity
|
|
humidity.Humidity = addHumidity.Humidity
|
|
|
|
var room models.Room
|
|
name := addHumidity.RoomName
|
|
if err := DB.Where("name = ?", name).First(&room).Error; err == gorm.ErrRecordNotFound {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": "record not found",
|
|
})
|
|
return
|
|
} else if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "error",
|
|
"error": err.Error,
|
|
})
|
|
}
|
|
|
|
humidity.RoomID = room.ID
|
|
|
|
result := DB.Create(&humidity)
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"status": "failure",
|
|
"error": "room already exists in dtm",
|
|
})
|
|
return
|
|
} else if result.Error != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"status": "failure",
|
|
"error": result.Error,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, humidity)
|
|
}
|