Widyarhn
2 years ago
commit
9a6653a6c9
13 changed files with 466 additions and 0 deletions
@ -0,0 +1,2 @@
|
||||
PORT=8000 |
||||
DB_URL="host=dumbo.db.elephantsql.com user=yvvpeiee password=QwAhcTtiKL8j9qRyUBIqs9kNbu3O0_FB dbname=yvvpeiee port=5432 sslmode=disable" |
@ -0,0 +1,111 @@
|
||||
package controllers |
||||
|
||||
import ( |
||||
"go-crud/initializers" |
||||
"go-crud/models" |
||||
"time" |
||||
|
||||
"github.com/gin-gonic/gin" |
||||
) |
||||
|
||||
func EventsCreate(c *gin.Context) { |
||||
// Get data off req body
|
||||
var body struct { |
||||
Name string |
||||
Content string |
||||
PostBy string |
||||
Category string |
||||
} |
||||
c.Bind(&body) |
||||
|
||||
// Create a event
|
||||
now := time.Now() |
||||
event := models.Event{ |
||||
Name: body.Name, |
||||
Content: body.Content, |
||||
PostBy: body.PostBy, |
||||
Category: body.Category, |
||||
Date: &now, |
||||
} |
||||
result := initializers.DB.Create(&event) |
||||
|
||||
if result.Error != nil { |
||||
c.Status(400) |
||||
return |
||||
} |
||||
// Return it
|
||||
c.JSON(200, gin.H{ |
||||
"event": event, |
||||
}) |
||||
} |
||||
|
||||
func EventsIndex(c *gin.Context) { |
||||
// Get the events
|
||||
var events []models.Event |
||||
initializers.DB.Find(&events) |
||||
|
||||
// Respond with them
|
||||
c.JSON(200, gin.H{ |
||||
"event": events, |
||||
}) |
||||
} |
||||
|
||||
func EventsShow(c *gin.Context) { |
||||
// Get id off URL
|
||||
id := c.Param("id") |
||||
|
||||
// Get the events
|
||||
var event models.Event |
||||
initializers.DB.First(&event, id) |
||||
|
||||
// Respond with them
|
||||
c.JSON(200, gin.H{ |
||||
"event": event, |
||||
}) |
||||
} |
||||
|
||||
func EventsUpdate(c *gin.Context) { |
||||
// Get the id off the URL
|
||||
id := c.Param("id") |
||||
|
||||
// Get the data off req body
|
||||
|
||||
var body struct { |
||||
Name string |
||||
Content string |
||||
PostBy string |
||||
Category string |
||||
Date *time.Time |
||||
} |
||||
c.Bind(&body) |
||||
|
||||
// Find the post were updating
|
||||
var event models.Event |
||||
initializers.DB.First(&event, id) |
||||
|
||||
// Update it
|
||||
now := time.Now() |
||||
initializers.DB.Model(&event).Updates(models.Event{ |
||||
Name: body.Name, |
||||
Content: body.Content, |
||||
PostBy: body.PostBy, |
||||
Category: body.Category, |
||||
Date: &now, |
||||
}) |
||||
|
||||
// Respond with it
|
||||
c.JSON(200, gin.H{ |
||||
"event": event, |
||||
}) |
||||
} |
||||
|
||||
func EventsDelete(c *gin.Context) { |
||||
// Get the id off the URL
|
||||
id := c.Param("id") |
||||
|
||||
// Delete the post
|
||||
initializers.DB.Delete(&models.Event{}, id) |
||||
|
||||
// Respond
|
||||
c.Status(200) |
||||
} |
@ -0,0 +1,93 @@
|
||||
package controllers |
||||
|
||||
import ( |
||||
"go-crud/initializers" |
||||
"go-crud/models" |
||||
|
||||
"github.com/gin-gonic/gin" |
||||
) |
||||
|
||||
func PostsCreate(c *gin.Context) { |
||||
// Get data off req body
|
||||
var body struct { |
||||
Body string |
||||
Title string |
||||
} |
||||
c.Bind(&body) |
||||
|
||||
// Create a post
|
||||
post := models.Post{Title: body.Title, Body: body.Body} |
||||
result := initializers.DB.Create(&post) |
||||
|
||||
if result.Error != nil { |
||||
c.Status(400) |
||||
return |
||||
} |
||||
// Return it
|
||||
c.JSON(200, gin.H{ |
||||
"post": post, |
||||
}) |
||||
} |
||||
|
||||
func PostsIndex(c *gin.Context) { |
||||
// Get the posts
|
||||
var posts []models.Post |
||||
initializers.DB.Find(&posts) |
||||
|
||||
// Respond with them
|
||||
c.JSON(200, gin.H{ |
||||
"post": posts, |
||||
}) |
||||
} |
||||
|
||||
func PostsShow(c *gin.Context) { |
||||
// Get id off URL
|
||||
id := c.Param("id") |
||||
|
||||
// Get the posts
|
||||
var post models.Post |
||||
initializers.DB.First(&post, id) |
||||
|
||||
// Respond with them
|
||||
c.JSON(200, gin.H{ |
||||
"post": post, |
||||
}) |
||||
} |
||||
|
||||
func PostsUpdate(c *gin.Context) { |
||||
// Get the id off the URL
|
||||
id := c.Param("id") |
||||
|
||||
// Get the data off req body
|
||||
var body struct { |
||||
Body string |
||||
Title string |
||||
} |
||||
c.Bind(&body) |
||||
|
||||
// Find the post were updating
|
||||
var post models.Post |
||||
initializers.DB.First(&post, id) |
||||
|
||||
// Update it
|
||||
initializers.DB.Model(&post).Updates(models.Post{ |
||||
Title: body.Title, |
||||
Body: body.Body, |
||||
}) |
||||
|
||||
// Respond with it
|
||||
c.JSON(200, gin.H{ |
||||
"post": post, |
||||
}) |
||||
} |
||||
|
||||
func PostsDelete(c *gin.Context) { |
||||
// Get the id off the URL
|
||||
id := c.Param("id") |
||||
|
||||
// Delete the post
|
||||
initializers.DB.Delete(&models.Post{}, id) |
||||
|
||||
// Respond
|
||||
c.Status(200) |
||||
} |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,45 @@
|
||||
module go-crud |
||||
|
||||
go 1.20 |
||||
|
||||
require gorm.io/driver/postgres v1.5.2 |
||||
|
||||
require ( |
||||
github.com/bytedance/sonic v1.9.2 // indirect |
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect |
||||
github.com/fatih/color v1.9.0 // indirect |
||||
github.com/fsnotify/fsnotify v1.4.9 // indirect |
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect |
||||
github.com/gin-contrib/sse v0.1.0 // indirect |
||||
github.com/gin-gonic/gin v1.9.1 // indirect |
||||
github.com/githubnemo/CompileDaemon v1.4.0 // indirect |
||||
github.com/go-playground/locales v0.14.1 // indirect |
||||
github.com/go-playground/universal-translator v0.18.1 // indirect |
||||
github.com/go-playground/validator/v10 v10.14.1 // indirect |
||||
github.com/goccy/go-json v0.10.2 // indirect |
||||
github.com/jackc/pgpassfile v1.0.0 // indirect |
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect |
||||
github.com/jackc/pgx/v5 v5.3.1 // indirect |
||||
github.com/jinzhu/inflection v1.0.0 // indirect |
||||
github.com/jinzhu/now v1.1.5 // indirect |
||||
github.com/joho/godotenv v1.5.1 // indirect |
||||
github.com/json-iterator/go v1.1.12 // indirect |
||||
github.com/klauspost/cpuid/v2 v2.2.5 // indirect |
||||
github.com/leodido/go-urn v1.2.4 // indirect |
||||
github.com/mattn/go-colorable v0.1.4 // indirect |
||||
github.com/mattn/go-isatty v0.0.19 // indirect |
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect |
||||
github.com/modern-go/reflect2 v1.0.2 // indirect |
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect |
||||
github.com/radovskyb/watcher v1.0.7 // indirect |
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect |
||||
github.com/ugorji/go/codec v1.2.11 // indirect |
||||
golang.org/x/arch v0.3.0 // indirect |
||||
golang.org/x/crypto v0.10.0 // indirect |
||||
golang.org/x/net v0.11.0 // indirect |
||||
golang.org/x/sys v0.9.0 // indirect |
||||
golang.org/x/text v0.10.0 // indirect |
||||
google.golang.org/protobuf v1.31.0 // indirect |
||||
gopkg.in/yaml.v3 v3.0.1 // indirect |
||||
gorm.io/gorm v1.25.2 // indirect |
||||
) |
@ -0,0 +1,111 @@
|
||||
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= |
||||
github.com/bytedance/sonic v1.9.2 h1:GDaNjuWSGu09guE9Oql0MSTNhNCLlWwO8y/xM5BzcbM= |
||||
github.com/bytedance/sonic v1.9.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= |
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= |
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= |
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= |
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= |
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= |
||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= |
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= |
||||
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= |
||||
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= |
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= |
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= |
||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= |
||||
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= |
||||
github.com/githubnemo/CompileDaemon v1.4.0 h1:z96Qu4tj+RzRfF+L7f1O6E8ion5JQlisWeXWc2wzwDQ= |
||||
github.com/githubnemo/CompileDaemon v1.4.0/go.mod h1:/G125r3YBIp6rcXtCZfiEHwFzcl7GSsNSwylxSNrkMA= |
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= |
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= |
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= |
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= |
||||
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k= |
||||
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= |
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= |
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= |
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= |
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= |
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= |
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= |
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= |
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= |
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= |
||||
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU= |
||||
github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8= |
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= |
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= |
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= |
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= |
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= |
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= |
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= |
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= |
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= |
||||
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= |
||||
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= |
||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= |
||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= |
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= |
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= |
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= |
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= |
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= |
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= |
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= |
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= |
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= |
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= |
||||
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= |
||||
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= |
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= |
||||
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= |
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= |
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= |
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= |
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= |
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= |
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= |
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= |
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= |
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= |
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= |
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= |
||||
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= |
||||
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= |
||||
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= |
||||
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= |
||||
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= |
||||
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= |
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= |
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= |
||||
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= |
||||
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= |
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= |
||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= |
||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
||||
gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0= |
||||
gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8= |
||||
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho= |
||||
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= |
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= |
@ -0,0 +1,22 @@
|
||||
package initializers |
||||
|
||||
import( |
||||
"log" |
||||
"os" |
||||
|
||||
"gorm.io/gorm" |
||||
"gorm.io/driver/postgres" |
||||
|
||||
)
|
||||
|
||||
var DB *gorm.DB |
||||
|
||||
func ConnectToDB(){ |
||||
var err error |
||||
dsn := os.Getenv("DB_URL") |
||||
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) |
||||
|
||||
if err != nil{ |
||||
log.Fatal("Failed to connect to database") |
||||
} |
||||
} |
@ -0,0 +1,13 @@
|
||||
package initializers |
||||
|
||||
import( |
||||
"log" |
||||
"github.com/joho/godotenv" |
||||
) |
||||
|
||||
func LoadEnvVariables(){ |
||||
err := godotenv.Load() |
||||
if err != nil{ |
||||
log.Fatal("Error loading .env file") |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"go-crud/controllers" |
||||
"go-crud/initializers" |
||||
|
||||
"github.com/gin-gonic/gin" |
||||
) |
||||
|
||||
func init() { |
||||
initializers.LoadEnvVariables() |
||||
initializers.ConnectToDB() |
||||
} |
||||
func main() { |
||||
r := gin.Default() |
||||
|
||||
r.POST("/posts", controllers.PostsCreate) |
||||
r.PUT("/posts/:id", controllers.PostsUpdate) |
||||
r.GET("/posts", controllers.PostsIndex) |
||||
r.GET("/posts/:id", controllers.PostsShow) |
||||
r.DELETE("/posts/:id", controllers.PostsDelete) |
||||
|
||||
r.POST("/events", controllers.EventsCreate) |
||||
r.PUT("/events/:id", controllers.EventsUpdate) |
||||
r.GET("/events", controllers.EventsIndex) |
||||
r.GET("/events/:id", controllers.EventsShow) |
||||
r.DELETE("/events/:id", controllers.EventsDelete) |
||||
|
||||
r.Run() // listen and serve on 0.0.0.0:8080
|
||||
} |
@ -0,0 +1,14 @@
|
||||
package main |
||||
|
||||
import "go-crud/initializers" |
||||
import "go-crud/models" |
||||
|
||||
func init() { |
||||
initializers.LoadEnvVariables() |
||||
initializers.ConnectToDB() |
||||
} |
||||
|
||||
func main() { |
||||
initializers.DB.AutoMigrate(&models.Post{}) |
||||
initializers.DB.AutoMigrate(&models.Event{}) |
||||
} |
@ -0,0 +1,16 @@
|
||||
package models |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"gorm.io/gorm" |
||||
) |
||||
|
||||
type Event struct { |
||||
gorm.Model |
||||
Name string |
||||
Content string |
||||
PostBy string |
||||
Category string |
||||
Date *time.Time |
||||
} |
Loading…
Reference in new issue