Skip to content

Commit

Permalink
Merge pull request #172 from SCCapstone/jackie-bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiehdinh authored Apr 24, 2023
2 parents 2e2c805 + 52b9f00 commit 859961f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 68 deletions.
148 changes: 89 additions & 59 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
Expand All @@ -11,7 +12,6 @@ import (
"os"
"strconv"
"strings"
"encoding/json"

middleware "github.com/SCCapstone/BitCrunch/middleware"
// models "github.com/SCCapstone/BitCrunch/models"
Expand Down Expand Up @@ -252,15 +252,15 @@ func displayModal(modalName string, msg string) gin.HandlerFunc {
fn := func(c *gin.Context) {
prevfloors, previmage, prevdevices, prevdevimages, prevposT, prevposL := getPreviousRender()
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Map",
"payload": prevfloors,
"Image": previmage,
"EditLayerButton": "EditLayerButton",
"devices": prevdevices,
"deviceImages": prevdevimages,
"title": "Map",
"payload": prevfloors,
"Image": previmage,
"EditLayerButton": "EditLayerButton",
"devices": prevdevices,
"deviceImages": prevdevimages,
"devicePositionsT": prevposT,
"devicePositionsL": prevposL,
modalName: msg,
modalName: msg,
})
}
return gin.HandlerFunc(fn)
Expand All @@ -286,8 +286,8 @@ Renders the proper floor image onto the map
*/
func viewLayer(c *gin.Context) {
name := c.PostForm("layer")
if(!(len(name) > 0)) {
if(!(len(getCurrentFloor()) > 0)) {
if !(len(name) > 0) {
if !(len(getCurrentFloor()) > 0) {
showMap(c)
return
} else {
Expand Down Expand Up @@ -352,18 +352,18 @@ func viewLayer(c *gin.Context) {
devicePositionsL = append(devicePositionsL, db.GetPositionsL((deviceNames[i]), getCurrentFloor()))
}

setPreviousRender(floorNames, "static/assets/" + imageName, deviceNames, deviceImages, devicePositionsT, devicePositionsL)
setPreviousRender(floorNames, "static/assets/"+imageName, deviceNames, deviceImages, devicePositionsT, devicePositionsL)

Render(c, gin.H{
"title": "Map",
"payload": floorNames,
"Image": "static/assets/" + imageName,
"EditLayerButton": "EditLayerButton",
"devices": deviceNames,
"deviceImages": deviceImages,
"title": "Map",
"payload": floorNames,
"Image": "static/assets/" + imageName,
"EditLayerButton": "EditLayerButton",
"devices": deviceNames,
"deviceImages": deviceImages,
"devicePositionsT": devicePositionsT,
"devicePositionsL": devicePositionsL,
"scripts": scriptNames,
"scripts": scriptNames,
}, "index.html")
}

Expand All @@ -382,26 +382,26 @@ func getPreviousRender() ([]string, string, []string, []string, []string, []stri

func viewDevice(c *gin.Context) {
name := c.PostForm("device")
if(len(name) > 0) {
if len(name) > 0 {
setCurrentDevice(name)
}
dragname := c.PostForm("dragbutton")
if(len(dragname) > 0) {
if len(dragname) > 0 {
setCurrentDevice(dragname)
}

prevfloors, previmage, prevdevices, prevdevimages, prevposT, prevposL := getPreviousRender()

c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Map",
"payload": prevfloors,
"Image": previmage,
"EditLayerButton": "EditLayerButton",
"devices": prevdevices,
"ViewDeviceModal": "ViewDeviceModal",
"DeviceName": getCurrentDevice(),
"DeviceIP": db.GetIP(getCurrentDevice(), getCurrentFloor()),
"deviceImages": prevdevimages,
"title": "Map",
"payload": prevfloors,
"Image": previmage,
"EditLayerButton": "EditLayerButton",
"devices": prevdevices,
"ViewDeviceModal": "ViewDeviceModal",
"DeviceName": getCurrentDevice(),
"DeviceIP": db.GetIP(getCurrentDevice(), getCurrentFloor()),
"deviceImages": prevdevimages,
"devicePositionsT": prevposT,
"devicePositionsL": prevposL,
})
Expand Down Expand Up @@ -486,13 +486,13 @@ func EditLayer(c *gin.Context) {
old_file_name := getCurrentFile()
layer_name := c.PostForm("layer_name")
fname := old_file_name
if (len(layer_name) == 0) {
if len(layer_name) == 0 {
layer_name = old_layer_name
}
file, err := c.FormFile("layer_image")
if (err != nil) {
if err != nil {
fmt.Println(err)

} else {
err = c.SaveUploadedFile(file, "static/assets/"+file.Filename)
fname = file.Filename
Expand Down Expand Up @@ -528,17 +528,46 @@ func AddDevice(c *gin.Context) {
device_image, err := c.FormFile("device_image")

if err != nil {
renderError(c, "AddDeviceModal", "Add Device Modal", "ErrorTitle", "Failed to Add Device", "ErrorMessage", "Image file could not be found.")
Render(c, gin.H{
"AddDeviceModal": "Add Device Modal",
"DeviceName": device_name,
"DeviceIP": device_ip,
"ErrorTitle": "Failed to Add Device",
"ErrorMessage": "Image file could not be found",
"EditLayerButton": "EditLayerButton"}, "index.html")
return
}
// set max image size to 2 MB
if device_image.Size > 2*1024*1024 {
Render(c, gin.H{
"AddDeviceModal": "Add Device Modal",
"DeviceName": device_name,
"DeviceIP": device_ip,
"ErrorTitle": "Failed to Add Device",
"ErrorMessage": "Image file is too large",
"EditLayerButton": "EditLayerButton"}, "index.html")
return
}
err = c.SaveUploadedFile(device_image, "static/assets/"+device_image.Filename)
if err != nil {
renderError(c, "AddDeviceModal", "Add Device Modal", "ErrorTitle", "Failed to Add Device", "ErrorMessage", "Image file could not be saved.")
Render(c, gin.H{
"AddDeviceModal": "Add Device Modal",
"DeviceName": device_name,
"DeviceIP": device_ip,
"ErrorTitle": "Failed to Add Device",
"ErrorMessage": "Image file could not be saved",
"EditLayerButton": "EditLayerButton"}, "index.html")
return
}

if _, err := db.CreateDevice(device_name, device_ip, "static/assets/"+device_image.Filename, getCurrentFloor()); err != nil {
renderError(c, "AddDeviceModal", "Add Device Modal", "ErrorTitle", "Failed to Add Device", "ErrorMessage", err.Error())
Render(c, gin.H{
"AddDeviceModal": "Add Device Modal",
"DeviceName": device_name,
"DeviceIP": device_ip,
"ErrorTitle": "Failed to Add Device",
"ErrorMessage": err.Error(),
"EditLayerButton": "EditLayerButton"}, "index.html")
return
}
showMap(c)
Expand Down Expand Up @@ -599,32 +628,32 @@ func editDevice(c *gin.Context) {

func changeDeviceCoordinates(c *gin.Context) {
bodyBytes, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
fmt.Println(err)
}
// Convert the byte array to a string
bodyString := string(bodyBytes)
// Print the JSON request to the terminal
fmt.Printf("JSON Request: %s\n", bodyString)
c.JSON(http.StatusOK, gin.H{"status": "ok"})

var data map[string]json.RawMessage
err = json.Unmarshal(bodyBytes, &data)
if err != nil {
log.Fatal(err)
}
topBytes := data["Top"]
leftBytes := data["Left"]
idBytes := data["ID"]
top := string(topBytes)
left := string(leftBytes)
id := string(idBytes)
id = removeQuotes(id)
db.EditDeviceCoordinates(id, getCurrentFloor(), top, left)
if err != nil {
fmt.Println(err)
}
// Convert the byte array to a string
bodyString := string(bodyBytes)
// Print the JSON request to the terminal
fmt.Printf("JSON Request: %s\n", bodyString)
c.JSON(http.StatusOK, gin.H{"status": "ok"})

var data map[string]json.RawMessage
err = json.Unmarshal(bodyBytes, &data)
if err != nil {
log.Fatal(err)
}
topBytes := data["Top"]
leftBytes := data["Left"]
idBytes := data["ID"]
top := string(topBytes)
left := string(leftBytes)
id := string(idBytes)
id = removeQuotes(id)
db.EditDeviceCoordinates(id, getCurrentFloor(), top, left)

}

func removeQuotes(s string) (string){
func removeQuotes(s string) string {
if len(s) > 0 && s[0] == '"' {
s = s[1:]
}
Expand Down Expand Up @@ -655,7 +684,7 @@ func pingDevice(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"Output": output,
})

}

func createDeviceFile(name string, filename string) {
Expand Down Expand Up @@ -734,3 +763,4 @@ func setCurrentDevice(deviceName string) {
func getCurrentDevice() (deviceName string) {
return currentDevice
}

21 changes: 16 additions & 5 deletions db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ func CreateUser(username, password, email string, admin int) (user, error) {
// Return specific error
return u, err
}
if err := checkEmail(email); err != nil {
if err := CheckEmail(email); err != nil {
// Return specific error
return u, err
}
if err := CheckEmailValid(email); err != nil {
return u, err
}

// Everything checks out so
// return the user and
Expand Down Expand Up @@ -252,17 +255,25 @@ func CheckPassword(password string) error {
}

/*
This function will check
to see that an email is valid
based on regex.
This function will check if email
is valid based on regex.
Returns nil if it is good to use.
*/
func checkEmail(e string) error {
func CheckEmailValid(e string) error {
// Checking for valid email
reg := regexp.MustCompile("(\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,6})")
if !reg.Match([]byte(e)) {
return fmt.Errorf("Please enter a valid email address.")
}
return nil
}

/*
This function will check if email is
already in database.
Returns nil if not in database.
*/
func CheckEmail(e string) error {
// Checking if email address already in use
fi, err := open(users)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ <h3>Are you sure you want to remove this layer?</h3>
<div class="modal-content">
<h3>Add Device</h3>
<form action="/u/add_device" method="POST" enctype="multipart/form-data">
Device Name: <input type="text" id="device_name" name="device_name"><br>
Device IP Address: <input type="text" id="device_ip" name="device_ip"><br>
Device Name: <input type="text" id="device_name" name="device_name" value = "{{ .DeviceName}}"><br>
Device IP Address: <input type="text" id="device_ip" name="device_ip" value = "{{ .DeviceIP}}"><br>
<input type="file" id="device_image" name="device_image" accept="image/*">
<input class="cancel_button" type="submit" value="Cancel" formaction="/u/view_layer" formmethod="post">
<input class="danger_button" type="submit" value="Save">
Expand Down
7 changes: 5 additions & 2 deletions templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1>Sign In</h1>

<form action="/u/login" method="POST">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<input type="text" id="username" name="username" value = "{{ .Username}}"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
{{ if .ErrorTitle}}
Expand All @@ -26,8 +26,11 @@ <h1>Sign In</h1>
</div>
</form>

<form action="/u/forgot-password" method="GET">
<p style = "margin-bottom: 1px"><input class="signup moveup" type="submit" value="Forgot Password?"></p>
</form>
<form action="/u/register" method="GET">
<p>Don't have an account? <input class="signup moveup" type="submit" value="Sign-up"></p>
<p style = "margin-top: 1px">Don't have an account? <input class="signup moveup" type="submit" value="Sign-up"></p>
</form>
</article>

Expand Down

0 comments on commit 859961f

Please sign in to comment.