Skip to content

Commit

Permalink
Images upload function
Browse files Browse the repository at this point in the history
- API Route for creating house
  • Loading branch information
kaizerpwn committed Oct 22, 2023
1 parent dbb8b29 commit c0144e7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
homelab-backend.exe
homelab-backend.exe~
homelab-backend.exe~
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
swag init --parseDependency && CompileDaemon -command="./homelab-backend"
CompileDaemon -command="./homelab-backend"

run-linux:
/home/kaizer/go/bin/CompileDaemon -command="./homelab-backend"
Expand Down
66 changes: 65 additions & 1 deletion controllers/Houses.controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package controllers

import (
"mime/multipart"
"net/http"

"github.com/gin-gonic/gin"
"github.com/kaizerpwn/homelab-backend/initializers"
"github.com/kaizerpwn/homelab-backend/models"
"github.com/kaizerpwn/homelab-backend/utils"
)

// @Summary Get House by ID
Expand All @@ -15,7 +17,7 @@ import (
// @Produce json
// @Param id path int true "House ID" Format(int64)
// @Success 200 {object} models.House
// @Failure 404 {string} string "Device with that ID doesn't exist."
// @Failure 404 {string} string "House with that ID doesn't exist."
// @Router /api/houses/{id} [get]
func GetHouseById(c *gin.Context) {
id := c.Param("id")
Expand All @@ -29,3 +31,65 @@ func GetHouseById(c *gin.Context) {
})
}
}

// @Summary Create House
// @Description Create new house with relevant data
// @Tags houses
// @Accept json
// @Produce json
// @Param id path int true "House ID" Format(int64)
// @Success 200 {string} string "Successfully added new house."
// @Failure 404 {string} string "Internal server error."
// @Router /api/houses/{id} [post]
func CreateHouse(c *gin.Context) {
var body struct {
Address string `json:"address" binding:"required"`
City string `json:"city" binding:"required"`
Country string `json:"country" binding:"required"`
ZipCode string `json:"zipcode" binding:"required"`
Floors int8 `json:"floors" binding:"required"`
SquareMeters float32 `json:"squareMeters" binding:"required"`
Latitude float32 `json:"latitude" binding:"required"`
Longitude float32 `json:"longitude" binding:"required"`
Image *multipart.FileHeader `form:"image" binding:"required"`
}

if err := c.Bind(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid request data",
})
return
}

var house models.House

var houseQueryData struct {
Longitude float32
Latitude float32
Address string
Floors int8
}

houseQueryData.Address = body.Address
houseQueryData.Longitude = body.Longitude
houseQueryData.Latitude = body.Latitude
houseQueryData.Floors = body.Floors

checkHouseExist := initializers.DB.First(&house, houseQueryData)

if checkHouseExist != nil {
c.JSON(http.StatusConflict, gin.H{
"message": "House with that data already exist.",
})
return
}

err := utils.UploadImage(c, body.Image)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal server error",
})
}

}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func init() {
func main() {
r := gin.Default()

// >> 8 MB Maximum upload file size
r.MaxMultipartMemory = 8 << 20

r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000"},
AllowMethods: []string{"PUT", "PATCH", "GET", "DELETE", "POST"},
Expand All @@ -49,6 +52,7 @@ func main() {
houses := v1.Group("/houses")
{
houses.GET("/:id", utils.VerifyToken, controllers.GetHouseById)
houses.POST("/", utils.VerifyToken, controllers.CreateHouse)
}

rooms := v1.Group("/rooms")
Expand Down
15 changes: 15 additions & 0 deletions utils/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import (
"mime/multipart"
"time"

"github.com/gin-gonic/gin"
)

func UploadImage(c *gin.Context, file *multipart.FileHeader) error {
currentTime := time.Now().Format("01-02-06 15-04-.999")

err := c.SaveUploadedFile(file, "assets/images"+currentTime+file.Filename)
return err
}

0 comments on commit c0144e7

Please sign in to comment.