+ Database insertion/overwriting

+ Helper
This commit is contained in:
Daniel Legt 2022-05-15 15:59:30 +03:00
parent e508d753ce
commit 0d71bf3650
5 changed files with 45 additions and 10 deletions

View File

@ -4,7 +4,8 @@ CREATE TABLE IF NOT EXISTS `t_posts` (
`content` MEDIUMTEXT NOT NULL COLLATE 'latin1_swedish_ci', `content` MEDIUMTEXT NOT NULL COLLATE 'latin1_swedish_ci',
`ts` DATETIME NOT NULL DEFAULT current_timestamp(), `ts` DATETIME NOT NULL DEFAULT current_timestamp(),
`ts_updated` DATETIME NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), `ts_updated` DATETIME NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `name` (`name`) USING BTREE
) )
COLLATE='latin1_swedish_ci' COLLATE='latin1_swedish_ci'
ENGINE=InnoDB ENGINE=InnoDB

View File

@ -11,11 +11,6 @@ var postList []*Post = []*Post{}
var postMap map[string]Post = make(map[string]Post) var postMap map[string]Post = make(map[string]Post)
func GetPostList() []*Post { func GetPostList() []*Post {
db := database.GetConn()
println(db)
return postList return postList
} }
@ -43,6 +38,23 @@ func Create(name string, content string) (*Post, error) {
postMap[name] = myPost postMap[name] = myPost
// Add the post to the database // Add the post to the database
db, err := database.GetConn()
if err != nil {
return nil, err
}
defer db.Close()
sql := `REPLACE INTO freepad.t_posts (name, content) VALUES (?, ?)`
s, err := db.Prepare(sql)
if err != nil {
return nil, err
}
_, err = s.Exec(myPost.Name, myPost.Content)
if err != nil {
return nil, err
}
// Return the post // Return the post
return &myPost, nil return &myPost, nil

14
src/helper/helper_main.go Normal file
View File

@ -0,0 +1,14 @@
package helper
import "os"
func GetDomainBase() string {
domainBase, domainExists := os.LookupEnv("DOMAIN_BASE")
if !domainExists {
os.Setenv("DOMAIN_BASE", "http://localhost:8080")
domainBase = "http://localhost:8080"
}
return domainBase
}

View File

@ -9,7 +9,7 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
func GetConn() *sql.DB { func GetConn() (*sql.DB, error) {
user := os.Getenv("MYSQL_USER") user := os.Getenv("MYSQL_USER")
password := os.Getenv("MYSQL_PASSWORD") password := os.Getenv("MYSQL_PASSWORD")
@ -18,7 +18,7 @@ func GetConn() *sql.DB {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s", user, password, dbname)) db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s", user, password, dbname))
if err != nil { if err != nil {
panic(err) return nil, err
} }
// Set options // Set options
@ -26,5 +26,5 @@ func GetConn() *sql.DB {
db.SetMaxOpenConns(10) db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10) db.SetMaxIdleConns(10)
return db return db, nil
} }

View File

@ -1,7 +1,11 @@
package routes package routes
import ( import (
"fmt"
"net/url"
"github.com/JustKato/FreePad/controllers/post" "github.com/JustKato/FreePad/controllers/post"
"github.com/JustKato/FreePad/helper"
"github.com/JustKato/FreePad/types" "github.com/JustKato/FreePad/types"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -30,7 +34,11 @@ func ApiRoutes(route *gin.RouterGroup) {
}) })
} }
ctx.JSON(200, myPost) ctx.JSON(200, gin.H{
"message": "Post succesfully created",
"post": myPost,
"link": fmt.Sprintf("%s/%s", helper.GetDomainBase(), url.QueryEscape(myPost.Name)),
})
}) })