2020-03-20 23:02:18 +00:00
package main
import (
2022-07-09 17:42:00 +00:00
"log"
"net/http"
2020-03-20 23:02:18 +00:00
"strconv"
2022-07-09 17:42:00 +00:00
"github.com/gin-gonic/gin"
2020-03-20 23:02:18 +00:00
)
2022-07-09 17:42:00 +00:00
func declareAPIAuthProposalsRoutes ( router * gin . RouterGroup ) {
router . GET ( "/proposals" , func ( c * gin . Context ) {
q := c . MustGet ( "question" ) . ( * Question )
proposals , err := q . GetProposals ( )
if err != nil {
log . Printf ( "Unable to GetProposals(qid=%d): %s" , q . Id , err . Error ( ) )
c . AbortWithStatusJSON ( http . StatusInternalServerError , gin . H { "errmsg" : "An error occurs during proposals retrieving" } )
return
2022-02-21 06:16:25 +00:00
}
2022-07-09 17:42:00 +00:00
c . JSON ( http . StatusOK , proposals )
} )
}
func declareAPIAdminProposalsRoutes ( router * gin . RouterGroup ) {
router . POST ( "/proposals" , func ( c * gin . Context ) {
q := c . MustGet ( "question" ) . ( * Question )
2022-02-21 06:16:25 +00:00
var new Proposal
2022-07-09 17:42:00 +00:00
if err := c . ShouldBindJSON ( & new ) ; err != nil {
c . AbortWithStatusJSON ( http . StatusBadRequest , gin . H { "errmsg" : err . Error ( ) } )
return
2022-02-21 06:16:25 +00:00
}
2022-07-09 17:42:00 +00:00
proposal , err := q . NewProposal ( new . Label )
if err != nil {
log . Printf ( "Unable to NewProposal(qid=%d): %s" , q . Id , err . Error ( ) )
c . AbortWithStatusJSON ( http . StatusInternalServerError , gin . H { "errmsg" : "An error occurs when trying to insert new proposal." } )
return
2020-03-20 23:02:18 +00:00
}
2022-07-09 17:42:00 +00:00
c . JSON ( http . StatusOK , proposal )
} )
proposalsRoutes := router . Group ( "/proposals/:pid" )
proposalsRoutes . Use ( proposalHandler )
proposalsRoutes . PUT ( "" , func ( c * gin . Context ) {
current := c . MustGet ( "proposal" ) . ( * Proposal )
2020-03-20 23:02:18 +00:00
var new Proposal
2022-07-09 17:42:00 +00:00
if err := c . ShouldBindJSON ( & new ) ; err != nil {
c . AbortWithStatusJSON ( http . StatusBadRequest , gin . H { "errmsg" : err . Error ( ) } )
return
2020-03-20 23:02:18 +00:00
}
new . Id = current . Id
2022-07-09 17:42:00 +00:00
proposal , err := new . Update ( )
if err != nil {
log . Println ( "Unable to Update proposal:" , err )
c . AbortWithStatusJSON ( http . StatusInternalServerError , gin . H { "errmsg" : "An error occurs during proposal updating." } )
return
}
2020-03-20 23:02:18 +00:00
2022-07-09 17:42:00 +00:00
c . JSON ( http . StatusOK , proposal )
} )
proposalsRoutes . DELETE ( "" , func ( c * gin . Context ) {
p := c . MustGet ( "proposal" ) . ( * Proposal )
if _ , err := p . Delete ( ) ; err != nil {
log . Printf ( "Unable to Delete(pid=%d) proposal: %s" , p . Id , err . Error ( ) )
c . AbortWithStatusJSON ( http . StatusInternalServerError , gin . H { "errmsg" : "An error occurs when trying to delete proposal." } )
return
2020-03-20 23:02:18 +00:00
}
2022-07-09 17:42:00 +00:00
c . JSON ( http . StatusOK , nil )
} )
}
func proposalHandler ( c * gin . Context ) {
var question * Question = nil
if q , ok := c . Get ( "question" ) ; ok {
question = q . ( * Question )
}
var proposal * Proposal
if pid , err := strconv . Atoi ( string ( c . Param ( "pid" ) ) ) ; err != nil {
c . AbortWithStatusJSON ( http . StatusBadRequest , gin . H { "errmsg" : "Invalid proposal ID" } )
return
} else if question == nil {
if proposal , err = getProposal ( pid ) ; err != nil {
c . AbortWithStatusJSON ( http . StatusNotFound , gin . H { "errmsg" : "Proposal not found" } )
return
}
} else {
if proposal , err = question . GetProposal ( pid ) ; err != nil {
c . AbortWithStatusJSON ( http . StatusNotFound , gin . H { "errmsg" : "Proposal not found" } )
return
2020-03-20 23:02:18 +00:00
}
}
2022-07-09 17:42:00 +00:00
if proposal == nil {
c . AbortWithStatusJSON ( http . StatusNotFound , gin . H { "errmsg" : "Proposal not found" } )
return
} else {
c . Set ( "proposal" , proposal )
c . Next ( )
2020-03-20 23:02:18 +00:00
}
}
type Proposal struct {
Id int64 ` json:"id" `
IdQuestion int64 ` json:"id_question" `
Label string ` json:"label" `
}
2022-07-09 17:42:00 +00:00
func ( q * Question ) GetProposals ( ) ( proposals [ ] * Proposal , err error ) {
2020-03-20 23:02:18 +00:00
if rows , errr := DBQuery ( "SELECT id_proposal, id_question, label FROM survey_proposals WHERE id_question=?" , q . Id ) ; errr != nil {
return nil , errr
} else {
defer rows . Close ( )
for rows . Next ( ) {
var p Proposal
if err = rows . Scan ( & p . Id , & p . IdQuestion , & p . Label ) ; err != nil {
return
}
2022-07-09 17:42:00 +00:00
proposals = append ( proposals , & p )
2020-03-20 23:02:18 +00:00
}
if err = rows . Err ( ) ; err != nil {
return
}
return
}
}
2022-07-09 17:42:00 +00:00
func getProposal ( id int ) ( p * Proposal , err error ) {
p = new ( Proposal )
2020-03-20 23:02:18 +00:00
err = DBQueryRow ( "SELECT id_proposal, id_question, label FROM survey_proposals WHERE id_proposal=?" , id ) . Scan ( & p . Id , & p . IdQuestion , & p . Label )
return
}
2022-07-09 17:42:00 +00:00
func ( q * Question ) GetProposal ( id int ) ( p * Proposal , err error ) {
p = new ( Proposal )
2020-03-20 23:02:18 +00:00
err = DBQueryRow ( "SELECT id_proposal, id_question, label FROM survey_proposals WHERE id_proposal=? AND id_question=?" , id , q . Id ) . Scan ( & p . Id , & p . IdQuestion , & p . Label )
return
}
func ( q * Question ) NewProposal ( label string ) ( Proposal , error ) {
if res , err := DBExec ( "INSERT INTO survey_proposals (id_question, label) VALUES (?, ?)" , q . Id , label ) ; err != nil {
return Proposal { } , err
} else if pid , err := res . LastInsertId ( ) ; err != nil {
return Proposal { } , err
} else {
return Proposal { pid , q . Id , label } , nil
}
}
func ( p Proposal ) Update ( ) ( Proposal , error ) {
_ , err := DBExec ( "UPDATE survey_proposals SET id_question = ?, label = ? WHERE id_proposal = ?" , p . IdQuestion , p . Label , p . Id )
return p , err
}
func ( p Proposal ) Delete ( ) ( int64 , error ) {
if res , err := DBExec ( "DELETE FROM survey_proposals WHERE id_proposal = ?" , p . Id ) ; err != nil {
return 0 , err
} else if nb , err := res . RowsAffected ( ) ; err != nil {
return 0 , err
} else {
return nb , err
}
}
func ClearProposals ( ) ( int64 , error ) {
if res , err := DBExec ( "DELETE FROM survey_proposals" ) ; err != nil {
return 0 , err
} else if nb , err := res . RowsAffected ( ) ; err != nil {
return 0 , err
} else {
return nb , err
}
}