Completed db rewrite
This commit is contained in:
parent
54d7d14ef9
commit
cf4b8e0119
19 changed files with 481 additions and 276 deletions
|
|
@ -1,16 +1,16 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
var db *sql.DB
|
||||
|
||||
func BindAPIs(r *gin.Engine, cfg_db *gorm.DB) error {
|
||||
func BindAPIs(r *gin.Engine, cfg_db *sql.DB) error {
|
||||
if cfg_db == nil {
|
||||
return errors.New("Database is null")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ func createExpense(c *gin.Context) {
|
|||
|
||||
if exp.Type == "user" {
|
||||
compare = func(m core.Member) bool {
|
||||
return m.UserID == exp.Payer
|
||||
return *m.UserId == exp.Payer
|
||||
}
|
||||
} else if exp.Type == "non-user" {
|
||||
compare = func(m core.Member) bool {
|
||||
|
|
@ -61,7 +61,7 @@ func createExpense(c *gin.Context) {
|
|||
}
|
||||
idx := slices.IndexFunc(members, compare)
|
||||
if idx != -1 {
|
||||
expense, err := core.CreateExpense(db, plan, members[idx], exp.Amount)
|
||||
expense, err := core.ExpenseCreate(db, *plan, members[idx], exp.Amount)
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -89,7 +89,7 @@ func listExpenses(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expenses, err := core.ListExpenses(db, plan)
|
||||
expenses, err := core.ExpensesList(db, *plan)
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -109,12 +109,12 @@ func getExpense(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expense, err := core.GetExpense(db, uint(expense_id))
|
||||
expense, err := core.ExpensesGet(db, uint(expense_id))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, err = u.GetPlan(db, expense.PlanID)
|
||||
_, err = u.GetPlan(db, expense.PlanId)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -135,12 +135,12 @@ func deleteExpense(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expense, err := core.GetExpense(db, uint(expense_id))
|
||||
expense, err := core.ExpensesGet(db, uint(expense_id))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, err = u.GetPlan(db, expense.PlanID)
|
||||
_, err = u.GetPlan(db, expense.PlanId)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -166,12 +166,12 @@ func getExpenseDebts(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expense, err := core.GetExpense(db, uint(expense_id))
|
||||
expense, err := core.ExpensesGet(db, uint(expense_id))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, err = u.GetPlan(db, expense.PlanID)
|
||||
_, err = u.GetPlan(db, expense.PlanId)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -198,17 +198,16 @@ func setExpenseDebts(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expense, err := core.GetExpense(db, uint(expense_id))
|
||||
expense, err := core.ExpensesGet(db, uint(expense_id))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
plan, err := u.GetPlan(db, expense.PlanID)
|
||||
_, err = u.GetPlan(db, expense.PlanId)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expense.Plan = plan
|
||||
|
||||
var debtSpec []core.DebtSpec
|
||||
if err := c.ShouldBind(&debtSpec); err != nil {
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"planner/core"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func extractUser(orm *gorm.DB, c *gin.Context) *core.User {
|
||||
func extractUser(orm *sql.DB, c *gin.Context) *core.User {
|
||||
username, _, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
return nil
|
||||
}
|
||||
u := core.User{
|
||||
Username: username,
|
||||
}
|
||||
|
||||
result := orm.Take(&u)
|
||||
if result.Error != nil {
|
||||
u, err := core.UserGet(db, username)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "Unable to find user "+username)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"planner/core"
|
||||
. "planner/core"
|
||||
)
|
||||
|
||||
|
|
@ -22,7 +23,7 @@ func createPlan(c *gin.Context) {
|
|||
}
|
||||
c.Bind(&plan_req)
|
||||
|
||||
_, err := CreatePlan(db, u, plan_req.Name)
|
||||
_, err := PlanCreate(db, u, plan_req.Name)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
|
|
@ -144,7 +145,7 @@ func joinPlan(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
plan, err := GetPlan(db, uint(plan_id))
|
||||
plan, err := PlanGet(db, uint(plan_id))
|
||||
if err != nil || plan == nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -171,7 +172,7 @@ func joinPlan(c *gin.Context) {
|
|||
c.String(http.StatusConflict, "User already a member")
|
||||
return
|
||||
}
|
||||
plan.AddMember(db, &Member{UserID: user.Username})
|
||||
plan.AddMember(db, &Member{UserId: &user.Username})
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
|
|
@ -212,10 +213,10 @@ func createPlanPoll(c *gin.Context) {
|
|||
}
|
||||
|
||||
poll := Poll{
|
||||
PlanID: plan.ID,
|
||||
PlanId: plan.Id,
|
||||
Options: poll_opts.Options,
|
||||
}
|
||||
db.Create(&poll)
|
||||
poll.Create(db)
|
||||
|
||||
c.JSON(http.StatusCreated, poll)
|
||||
}
|
||||
|
|
@ -236,8 +237,11 @@ func listPlanPolls(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var polls []Poll
|
||||
db.Where("plan_id = ?", params.Id).Find(&polls)
|
||||
polls, err := core.PollsList(db, int(params.Id))
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, polls)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ func getPoll(c *gin.Context) {
|
|||
}
|
||||
fmt.Println(params)
|
||||
|
||||
poll, _ := core.GetPoll(db, *user, params.PollId)
|
||||
poll, _ := core.PollGet(db, *user, params.PollId)
|
||||
c.JSON(http.StatusOK, poll)
|
||||
}
|
||||
|
||||
|
|
@ -45,9 +45,12 @@ func getPollVotes(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var votes []core.Vote
|
||||
db.Where("poll_id = ?", params.PollId).Find(&votes)
|
||||
c.JSON(http.StatusOK, &votes)
|
||||
votes, err := core.VotesList(db, int(params.PollId))
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, votes)
|
||||
}
|
||||
|
||||
func pollVote(c *gin.Context) {
|
||||
|
|
@ -66,7 +69,7 @@ func pollVote(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
poll, err := core.GetPoll(db, *user, path_params.PollId)
|
||||
poll, err := core.PollGet(db, *user, path_params.PollId)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
|
@ -77,7 +80,19 @@ func pollVote(c *gin.Context) {
|
|||
}
|
||||
c.Bind(&vote_params)
|
||||
|
||||
if err := poll.SetVote(db, *user, vote_params.Vote); err != nil {
|
||||
plan, err := core.PlanGet(db, poll.PlanId)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Unable to find plan: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
member, err := user.GetMemberFromPlan(db, *plan)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Unable to find member: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := poll.SetVote(db, *member, vote_params.Vote); err != nil {
|
||||
c.String(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
. "planner/core"
|
||||
"planner/core"
|
||||
)
|
||||
|
||||
func getUserByName(c *gin.Context) {
|
||||
|
|
@ -14,19 +14,23 @@ func getUserByName(c *gin.Context) {
|
|||
Name string `fdb:"name"`
|
||||
}
|
||||
if c.ShouldBind(&q) == nil {
|
||||
user := User{
|
||||
Username: q.Name,
|
||||
user, err := core.UserGet(db, q.Name)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Unable to get user: "+err.Error())
|
||||
return
|
||||
}
|
||||
db.Take(&user)
|
||||
fmt.Println(user)
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
}
|
||||
|
||||
func createUser(c *gin.Context) {
|
||||
var u User
|
||||
var u core.User
|
||||
if c.ShouldBind(&u) == nil {
|
||||
db.Create(&u)
|
||||
err := u.Create(db)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Unable to create user: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusCreated)
|
||||
} else {
|
||||
fmt.Print("Could not bind model")
|
||||
|
|
@ -43,12 +47,13 @@ func login(c *gin.Context) {
|
|||
if q.Username == "" {
|
||||
c.String(http.StatusBadRequest, "Login data is null")
|
||||
} else {
|
||||
user := User{
|
||||
Username: q.Username,
|
||||
user, err := core.UserGet(db, q.Username)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Unable to get user: "+err.Error())
|
||||
return
|
||||
}
|
||||
db.Take(&user)
|
||||
if user.Password == q.Password {
|
||||
c.JSON(http.StatusOK, map[string]string{"username": user.Username})
|
||||
c.JSON(http.StatusOK, gin.H{"username": user.Username})
|
||||
} else {
|
||||
c.Status(http.StatusForbidden)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue