fix: verification cleanup

This commit is contained in:
Reinaldy Rafli 2021-09-28 11:29:39 +07:00
parent a5c665fe4f
commit ae11ee7ed8
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
8 changed files with 71 additions and 57 deletions

View File

@ -1,4 +1,4 @@
FROM golang:1.16.6-buster
FROM golang:1.17-buster
WORKDIR /app

View File

@ -29,14 +29,16 @@ func New() *fiber.App {
// Setup PostgreSQL
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalln("Unable to create pool config", err)
log.Panicln("Unable to create pool config", err)
}
poolConfig.MaxConnIdleTime = time.Minute * 3
poolConfig.MaxConnLifetime = time.Minute * 5
poolConfig.MaxConns = 15
poolConfig.MinConns = 2
poolConfig.MinConns = 4
db, err := pgxpool.ConnectConfig(ctx, poolConfig)
if err != nil {
log.Fatalln("Unable to create connection", err)
log.Panicln("Unable to create connection", err)
}
// Setup Redis
@ -49,7 +51,7 @@ func New() *fiber.App {
// Setup In Memory
memory, err := bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
if err != nil {
log.Fatalln(err)
log.Panicln(err)
}
// Setup Sentry
@ -62,23 +64,23 @@ func New() *fiber.App {
Debug: true,
})
if err != nil {
log.Fatalln(err)
log.Panicln(err)
}
defer sentry.Flush(2 * time.Second)
err = database.Setup(db, &ctx)
if err != nil {
sentry.CaptureException(err)
log.Fatalln(err)
log.Panicln(err)
}
err = core.SetAllJSONJoke(db, memory, &ctx)
if err != nil {
log.Fatalln(err)
log.Panicln(err)
}
err = core.SetTotalJoke(db, memory, &ctx)
if err != nil {
log.Fatalln(err)
log.Panicln(err)
}
timeoutDefault := time.Minute * 1

View File

@ -49,14 +49,13 @@ func (d *Dependencies) AddNewJoke(c *fiber.Ctx) error {
if err != nil {
return err
}
v, err := conn.Query(*d.Context, sql, args...)
var validateLink string
err = conn.QueryRow(*d.Context, sql, args...).Scan(&validateLink)
if err != nil && err != pgx.ErrNoRows {
return err
}
defer v.Close()
if err == nil {
if err == nil && validateLink != "" {
return c.Status(fiber.StatusConflict).JSON(Error{
Error: "Given link is already on the jokesbapak2 database",
})

View File

@ -2,6 +2,7 @@ package joke
import (
"jokes-bapak2-api/app/core"
"strconv"
"github.com/Masterminds/squirrel"
"github.com/gofiber/fiber/v2"
@ -26,13 +27,13 @@ func (d *Dependencies) UpdateJoke(c *fiber.Ctx) error {
return err
}
var jokeID string
var jokeID int
err = conn.QueryRow(*d.Context, sql, args...).Scan(&jokeID)
if err != nil && err != pgx.ErrNoRows {
return err
}
if jokeID == id {
if strconv.Itoa(jokeID) == id {
body := new(core.Joke)
err = c.BodyParser(&body)
if err != nil {

View File

@ -83,13 +83,13 @@ func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error {
return err
}
v, err := conn.Query(*d.Context, sql, args...)
var validateLink string
err = conn.QueryRow(*d.Context, sql, args...).Scan(&validateLink)
if err != nil && err != pgx.ErrNoRows {
return err
}
defer v.Close()
if err == nil {
if err == nil && validateLink != "" {
return c.Status(fiber.StatusConflict).JSON(Error{
Error: "Given link is already on the submission queue.",
})

View File

@ -2,7 +2,7 @@ package submit
import (
"bytes"
"log"
"net/url"
"strconv"
"github.com/aldy505/bob"
@ -58,7 +58,11 @@ func (d *Dependencies) GetSubmission(c *fiber.Ctx) error {
if query.Author != "" {
sqlQuery.WriteString(" AND author = ?")
args = append(args, query.Author)
escapedAuthor, err := url.QueryUnescape(query.Author)
if err != nil {
return err
}
args = append(args, escapedAuthor)
}
if query.Approved != "" {
@ -81,7 +85,6 @@ func (d *Dependencies) GetSubmission(c *fiber.Ctx) error {
var submissions []Submission
results, err := d.DB.Query(*d.Context, sql, args...)
if err != nil {
log.Println(err)
return err
}

View File

@ -12,6 +12,7 @@ import (
func Setup(db *pgxpool.Pool, ctx *context.Context) error {
conn, err := db.Acquire(*ctx)
if err != nil {
log.Fatalln("30 - err here")
return err
}
defer conn.Release()
@ -41,18 +42,26 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
return err
}
_, err = conn.Query(*ctx, sql)
q, err := conn.Query(*ctx, sql)
if err != nil {
log.Fatalln("18 - failed on table creation: ", err)
return err
}
defer q.Close()
}
conn2, err := db.Acquire(*ctx)
if err != nil {
log.Fatalln("32 - err here")
return err
}
defer conn2.Release()
// Jokesbapak2 table
// Check if table exists
var tableJokesExists bool
err = conn.QueryRow(*ctx, `SELECT EXISTS (
err = conn2.QueryRow(*ctx, `SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'jokesbapak2'
@ -74,22 +83,28 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
return err
}
_, err = conn.Query(*ctx, sql)
q, err := conn2.Query(*ctx, sql)
if err != nil {
log.Fatalln("12 - failed on table creation: ", err)
return err
}
defer q.Close()
}
// Submission table
conn3, err := db.Acquire(*ctx)
if err != nil {
return err
}
defer conn3.Release()
//Check if table exists
var tableSubmissionExists bool
err = conn.QueryRow(*ctx, `SELECT EXISTS (
err = conn3.QueryRow(*ctx, `SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'submission'
);`).Scan(&tableJokesExists)
);`).Scan(&tableSubmissionExists)
if err != nil {
log.Fatalln("13 - failed on checking table: ", err)
return err
@ -108,10 +123,11 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error {
log.Fatalln("14 - failed on table creation: ", err)
}
_, err = conn.Query(*ctx, sql)
q, err := conn3.Query(*ctx, sql)
if err != nil {
log.Fatalln("15 - failed on table creation: ", err)
}
defer q.Close()
}
return nil

View File

@ -23,7 +23,7 @@ func TestMain(m *testing.M) {
log.Println("---- Preparing for integration test")
err := setup()
if err != nil {
log.Fatalln(err)
log.Panicln(err)
}
log.Println("---- Preparation complete")
log.Print("\n")
@ -37,8 +37,6 @@ func setup() error {
if err != nil {
return errors.New("Unable to create pool config: " + err.Error())
}
poolConfig.MaxConns = 15
poolConfig.MinConns = 2
db, err := pgxpool.ConnectConfig(ctx, poolConfig)
if err != nil {
@ -46,32 +44,27 @@ func setup() error {
}
defer db.Close()
conn, err := db.Acquire(ctx)
dj, err := db.Query(ctx, "DROP TABLE \"jokesbapak2\"")
if err != nil {
log.Println("busy here - 57")
return err
}
defer dj.Close()
ds, err := db.Query(ctx, "DROP TABLE \"submission\"")
if err != nil {
log.Println("busy here - 67")
return err
}
defer ds.Close()
da, err := db.Query(ctx, "DROP TABLE \"administrators\"")
if err != nil {
log.Println("busy here - 62")
return err
}
defer conn.Release()
// dj, err := conn.Query(ctx, "DROP TABLE \"jokesbapak2\"")
// if err != nil {
// log.Println("busy here - 57")
// return err
// }
// defer dj.Close()
// ds, err := conn.Query(ctx, "DROP TABLE \"submission\"")
// if err != nil {
// log.Println("busy here - 67")
// return err
// }
// defer ds.Close()
// da, err := conn.Query(ctx, "DROP TABLE \"administrators\"")
// if err != nil {
// log.Println("busy here - 62")
// return err
// }
// defer da.Close()
defer da.Close()
err = database.Setup(db, &ctx)
if err != nil {
@ -79,19 +72,19 @@ func setup() error {
return err
}
ia, err := conn.Query(ctx, "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);", administratorsData...)
ia, err := db.Query(ctx, "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);", administratorsData...)
if err != nil {
return err
}
defer ia.Close()
ij, err := conn.Query(ctx, "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...)
ij, err := db.Query(ctx, "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...)
if err != nil {
return err
}
defer ij.Close()
is, err := conn.Query(ctx, "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...)
is, err := db.Query(ctx, "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...)
if err != nil {
return err
}