Compare commits

..

No commits in common. "0749d8ce7a98925f75b08baac10354cb0d06af79" and "ef86deb222cd431220b750cdd4e424dc4c05c180" have entirely different histories.

6 changed files with 126 additions and 128 deletions

View File

@ -9,7 +9,7 @@ import (
) )
const ( const (
TestAllowanceName = "Test History" TestGoalName = "Test Goal"
) )
func startServer(t *testing.T) *httpexpect.Expect { func startServer(t *testing.T) *httpexpect.Expect {
@ -49,56 +49,56 @@ func TestGetUserBadId(t *testing.T) {
e.GET("/user/bad-id").Expect().Status(400) e.GET("/user/bad-id").Expect().Status(400)
} }
func TestGetUserAllowanceWhenNoAllowancePresent(t *testing.T) { func TestGetUserGoalsWhenNoGoalsPresent(t *testing.T) {
e := startServer(t) e := startServer(t)
result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array() result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array()
result.Length().IsEqual(0) result.Length().IsEqual(0)
} }
func TestGetUserAllowance(t *testing.T) { func TestGetUserGoals(t *testing.T) {
e := startServer(t) e := startServer(t)
// Create a new allowance // Create a new goal
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestAllowanceName, "name": TestGoalName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
e.POST("/user/1/allowance").WithJSON(requestBody).Expect().Status(201) e.POST("/user/1/goals").WithJSON(requestBody).Expect().Status(201)
// Validate allowance // Validate goal
result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array() result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array()
result.Length().IsEqual(1) result.Length().IsEqual(1)
item := result.Value(0).Object() item := result.Value(0).Object()
item.Value("id").IsEqual(1) item.Value("id").IsEqual(1)
item.Value("name").IsEqual(TestAllowanceName) item.Value("name").IsEqual(TestGoalName)
item.Value("target").IsEqual(5000) item.Value("target").IsEqual(5000)
item.Value("weight").IsEqual(10) item.Value("weight").IsEqual(10)
item.Value("progress").IsEqual(0) item.Value("progress").IsEqual(0)
item.NotContainsKey("user_id") item.NotContainsKey("user_id")
} }
func TestGetUserAllowanceNoUser(t *testing.T) { func TestGetUserGoalsNoUser(t *testing.T) {
e := startServer(t) e := startServer(t)
e.GET("/user/999/allowance").Expect().Status(404) e.GET("/user/999/goals").Expect().Status(404)
} }
func TestGetUserAllowanceBadId(t *testing.T) { func TestGetUserGoalsBadId(t *testing.T) {
e := startServer(t) e := startServer(t)
e.GET("/user/bad-id/allowance").Expect().Status(400) e.GET("/user/bad-id/goals").Expect().Status(400)
} }
func TestCreateUserAllowance(t *testing.T) { func TestCreateUserGoal(t *testing.T) {
e := startServer(t) e := startServer(t)
// Create a new allowance // Create a new goal
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestAllowanceName, "name": TestGoalName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
response := e.POST("/user/1/allowance"). response := e.POST("/user/1/goals").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(201). Status(201).
@ -106,40 +106,40 @@ func TestCreateUserAllowance(t *testing.T) {
// Verify the response has an ID // Verify the response has an ID
response.ContainsKey("id") response.ContainsKey("id")
allowanceId := response.Value("id").Number().Raw() goalId := response.Value("id").Number().Raw()
// Verify the allowance exists in the list of allowances // Verify the goal exists in the list of goals
allowances := e.GET("/user/1/allowance"). goals := e.GET("/user/1/goals").
Expect(). Expect().
Status(200). Status(200).
JSON().Array() JSON().Array()
allowances.Length().IsEqual(1) goals.Length().IsEqual(1)
allowance := allowances.Value(0).Object() goal := goals.Value(0).Object()
allowance.Value("id").IsEqual(allowanceId) goal.Value("id").IsEqual(goalId)
allowance.Value("name").IsEqual(TestAllowanceName) goal.Value("name").IsEqual(TestGoalName)
allowance.Value("target").IsEqual(5000) goal.Value("target").IsEqual(5000)
allowance.Value("weight").IsEqual(10) goal.Value("weight").IsEqual(10)
allowance.Value("progress").IsEqual(0) goal.Value("progress").IsEqual(0)
} }
func TestCreateUserAllowanceNoUser(t *testing.T) { func TestCreateUserGoalNoUser(t *testing.T) {
e := startServer(t) e := startServer(t)
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestAllowanceName, "name": TestGoalName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
e.POST("/user/999/allowance"). e.POST("/user/999/goals").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(404) Status(404)
} }
func TestCreateUserAllowanceInvalidInput(t *testing.T) { func TestCreateUserGoalInvalidInput(t *testing.T) {
e := startServer(t) e := startServer(t)
// Test with empty name // Test with empty name
@ -149,7 +149,7 @@ func TestCreateUserAllowanceInvalidInput(t *testing.T) {
"weight": 10, "weight": 10,
} }
e.POST("/user/1/allowance"). e.POST("/user/1/goals").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(400) Status(400)
@ -159,76 +159,76 @@ func TestCreateUserAllowanceInvalidInput(t *testing.T) {
"target": 5000, "target": 5000,
} }
e.POST("/user/1/allowance"). e.POST("/user/1/goals").
WithJSON(invalidRequest). WithJSON(invalidRequest).
Expect(). Expect().
Status(400) Status(400)
} }
func TestCreateUserAllowanceBadId(t *testing.T) { func TestCreateUserGoalBadId(t *testing.T) {
e := startServer(t) e := startServer(t)
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestAllowanceName, "name": TestGoalName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
e.POST("/user/bad-id/allowance"). e.POST("/user/bad-id/goals").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(400) Status(400)
} }
func TestDeleteUserAllowance(t *testing.T) { func TestDeleteUserGoal(t *testing.T) {
e := startServer(t) e := startServer(t)
// Create a new allowance to delete // Create a new goal to delete
createRequest := map[string]interface{}{ createRequest := map[string]interface{}{
"name": TestAllowanceName, "name": TestGoalName,
"target": 1000, "target": 1000,
"weight": 5, "weight": 5,
} }
response := e.POST("/user/1/allowance"). response := e.POST("/user/1/goals").
WithJSON(createRequest). WithJSON(createRequest).
Expect(). Expect().
Status(201). Status(201).
JSON().Object() JSON().Object()
allowanceId := response.Value("id").Number().Raw() goalId := response.Value("id").Number().Raw()
// Delete the allowance // Delete the goal
e.DELETE("/user/1/allowance/" + strconv.Itoa(int(allowanceId))). e.DELETE("/user/1/goal/" + strconv.Itoa(int(goalId))).
Expect(). Expect().
Status(200). Status(200).
JSON().Object().Value("message").IsEqual("History deleted successfully") JSON().Object().Value("message").IsEqual("Goal deleted successfully")
// Verify the allowance no longer exists // Verify the goal no longer exists
allowances := e.GET("/user/1/allowance"). goals := e.GET("/user/1/goals").
Expect(). Expect().
Status(200). Status(200).
JSON().Array() JSON().Array()
allowances.Length().IsEqual(0) goals.Length().IsEqual(0)
} }
func TestDeleteUserAllowanceNotFound(t *testing.T) { func TestDeleteUserGoalNotFound(t *testing.T) {
e := startServer(t) e := startServer(t)
// Attempt to delete a non-existent allowance // Attempt to delete a non-existent goal
e.DELETE("/user/1/allowance/999"). e.DELETE("/user/1/goal/999").
Expect(). Expect().
Status(404). Status(404).
JSON().Object().Value("error").IsEqual("History not found") JSON().Object().Value("error").IsEqual("Goal not found")
} }
func TestDeleteUserAllowanceInvalidId(t *testing.T) { func TestDeleteUserGoalInvalidId(t *testing.T) {
e := startServer(t) e := startServer(t)
// Attempt to delete an allowance with an invalid ID // Attempt to delete a goal with an invalid ID
e.DELETE("/user/1/allowance/invalid-id"). e.DELETE("/user/1/goal/invalid-id").
Expect(). Expect().
Status(400). Status(400).
JSON().Object().Value("error").IsEqual("Invalid allowance ID") JSON().Object().Value("error").IsEqual("Invalid goal ID")
} }
func TestCreateTask(t *testing.T) { func TestCreateTask(t *testing.T) {
@ -323,7 +323,7 @@ func createTestTask(e *httpexpect.Expect) {
e.POST("/tasks").WithJSON(requestBody).Expect().Status(201) e.POST("/tasks").WithJSON(requestBody).Expect().Status(201)
} }
func TestGetTasksWhenTasks(t *testing.T) { func TestGetTaskSWhenTasks(t *testing.T) {
e := startServer(t) e := startServer(t)
createTestTask(e) createTestTask(e)
@ -391,9 +391,9 @@ func TestPutTaskInvalidTaskId(t *testing.T) {
func TestPostAllowance(t *testing.T) { func TestPostAllowance(t *testing.T) {
e := startServer(t) e := startServer(t)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200) e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200) e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200) e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200)
response := e.GET("/user/1").Expect().Status(200).JSON().Object() response := e.GET("/user/1").Expect().Status(200).JSON().Object()
response.Value("allowance").Number().IsEqual(100 + 20 - 10) response.Value("allowance").Number().IsEqual(100 + 20 - 10)
@ -402,16 +402,16 @@ func TestPostAllowance(t *testing.T) {
func TestPostAllowanceInvalidUserId(t *testing.T) { func TestPostAllowanceInvalidUserId(t *testing.T) {
e := startServer(t) e := startServer(t)
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).Expect(). e.POST("/user/999/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().
Status(404) Status(404)
} }
func TestGetHistory(t *testing.T) { func TestGetHistory(t *testing.T) {
e := startServer(t) e := startServer(t)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200) e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200) e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200) e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200)
response := e.GET("/user/1/history").Expect().Status(200).JSON().Array() response := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
response.Length().IsEqual(3) response.Length().IsEqual(3)

View File

@ -67,27 +67,27 @@ func (db *Db) UserExists(userId int) (bool, error) {
return count > 0, nil return count > 0, nil
} }
func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) { func (db *Db) GetUserGoals(userId int) ([]Goal, error) {
allowances := make([]Allowance, 0) goals := make([]Goal, 0)
var err error var err error
for row := range db.db.Query("select id, name, target, progress, weight from allowances where user_id = ?"). for row := range db.db.Query("select id, name, target, progress, weight from goals where user_id = ?").
Bind(userId).Range(&err) { Bind(userId).Range(&err) {
allowance := Allowance{} goal := Goal{}
err = row.Scan(&allowance.ID, &allowance.Name, &allowance.Target, &allowance.Progress, &allowance.Weight) err = row.Scan(&goal.ID, &goal.Name, &goal.Target, &goal.Progress, &goal.Weight)
if err != nil { if err != nil {
return nil, err return nil, err
} }
allowances = append(allowances, allowance) goals = append(goals, goal)
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
return allowances, nil return goals, nil
} }
func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (int, error) { func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
// Check if user exists before attempting to create an allowance // Check if user exists before attempting to create a goal
exists, err := db.UserExists(userId) exists, err := db.UserExists(userId)
if err != nil { if err != nil {
return 0, err return 0, err
@ -102,9 +102,9 @@ func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (in
} }
defer tx.MustRollback() defer tx.MustRollback()
// Insert the new allowance // Insert the new goal
err = tx.Query("insert into allowances (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)"). err = tx.Query("insert into goals (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
Bind(userId, allowance.Name, allowance.Target, allowance.Weight). Bind(userId, goal.Name, goal.Target, goal.Weight).
Exec() Exec()
if err != nil { if err != nil {
@ -127,21 +127,21 @@ func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (in
return lastId, nil return lastId, nil
} }
func (db *Db) DeleteAllowance(userId int, allowanceId int) error { func (db *Db) DeleteGoal(userId int, goalId int) error {
// Check if the allowance exists for the user // Check if the goal exists for the user
count := 0 count := 0
err := db.db.Query("select count(*) from allowances where id = ? and user_id = ?"). err := db.db.Query("select count(*) from goals where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&count) Bind(goalId, userId).ScanSingle(&count)
if err != nil { if err != nil {
return err return err
} }
if count == 0 { if count == 0 {
return errors.New("allowance not found") return errors.New("goal not found")
} }
// Delete the allowance // Delete the goal
err = db.db.Query("delete from allowances where id = ? and user_id = ?"). err = db.db.Query("delete from goals where id = ? and user_id = ?").
Bind(allowanceId, userId).Exec() Bind(goalId, userId).Exec()
if err != nil { if err != nil {
return err return err
} }
@ -236,7 +236,7 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
return tx.Commit() return tx.Commit()
} }
func (db *Db) AddHistory(userId int, allowance *PostHistory) error { func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error {
tx, err := db.db.Begin() tx, err := db.db.Begin()
if err != nil { if err != nil {
return err return err
@ -252,13 +252,13 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
return tx.Commit() return tx.Commit()
} }
func (db *Db) GetHistory(userId int) ([]History, error) { func (db *Db) GetHistory(userId int) ([]Allowance, error) {
history := make([]History, 0) history := make([]Allowance, 0)
var err error var err error
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc"). for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) { Bind(userId).Range(&err) {
allowance := History{} allowance := Allowance{}
var timestamp int64 var timestamp int64
err = row.Scan(&allowance.Allowance, &timestamp) err = row.Scan(&allowance.Allowance, &timestamp)
if err != nil { if err != nil {

View File

@ -13,12 +13,12 @@ type UserWithAllowance struct {
Allowance int `json:"allowance"` Allowance int `json:"allowance"`
} }
type History struct { type Allowance struct {
Allowance int `json:"allowance"` Allowance int `json:"allowance"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
} }
type PostHistory struct { type PostAllowance struct {
Allowance int `json:"allowance"` Allowance int `json:"allowance"`
} }
@ -30,7 +30,7 @@ type Task struct {
Assigned *int `json:"assigned"` // Pointer to allow null Assigned *int `json:"assigned"` // Pointer to allow null
} }
type Allowance struct { type Goal struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target int `json:"target"`
@ -38,7 +38,7 @@ type Allowance struct {
Weight int `json:"weight"` Weight int `json:"weight"`
} }
type CreateAllowanceRequest struct { type CreateGoalRequest struct {
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target int `json:"target"`
Weight int `json:"weight"` Weight int `json:"weight"`

View File

@ -76,7 +76,7 @@ func getUser(c *gin.Context) {
c.IndentedJSON(http.StatusOK, user) c.IndentedJSON(http.StatusOK, user)
} }
func getUserAllowance(c *gin.Context) { func getUserGoals(c *gin.Context) {
userIdStr := c.Param("userId") userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr) userId, err := strconv.Atoi(userIdStr)
if err != nil { if err != nil {
@ -97,16 +97,16 @@ func getUserAllowance(c *gin.Context) {
return return
} }
allowances, err := db.GetUserAllowances(userId) goals, err := db.GetUserGoals(userId)
if err != nil { if err != nil {
log.Printf("Error getting user allowance: %v", err) log.Printf("Error getting user goals: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError}) c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return return
} }
c.IndentedJSON(http.StatusOK, allowances) c.IndentedJSON(http.StatusOK, goals)
} }
func createUserAllowance(c *gin.Context) { func createUserGoal(c *gin.Context) {
userIdStr := c.Param("userId") userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr) userId, err := strconv.Atoi(userIdStr)
if err != nil { if err != nil {
@ -116,7 +116,7 @@ func createUserAllowance(c *gin.Context) {
} }
// Parse request body // Parse request body
var goalRequest CreateAllowanceRequest var goalRequest CreateGoalRequest
if err := c.ShouldBindJSON(&goalRequest); err != nil { if err := c.ShouldBindJSON(&goalRequest); err != nil {
log.Printf("Error parsing request body: %v", err) log.Printf("Error parsing request body: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
@ -125,12 +125,12 @@ func createUserAllowance(c *gin.Context) {
// Validate request // Validate request
if goalRequest.Name == "" { if goalRequest.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Allowance name cannot be empty"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Goal name cannot be empty"})
return return
} }
// Create goal in database // Create goal in database
goalId, err := db.CreateAllowance(userId, &goalRequest) goalId, err := db.CreateGoal(userId, &goalRequest)
if err != nil { if err != nil {
log.Printf("Error creating goal: %v", err) log.Printf("Error creating goal: %v", err)
if err.Error() == "user does not exist" { if err.Error() == "user does not exist" {
@ -146,9 +146,9 @@ func createUserAllowance(c *gin.Context) {
c.IndentedJSON(http.StatusCreated, response) c.IndentedJSON(http.StatusCreated, response)
} }
func deleteUserAllowance(c *gin.Context) { func deleteUserGoal(c *gin.Context) {
userIdStr := c.Param("userId") userIdStr := c.Param("userId")
allowanceIdStr := c.Param("allowanceId") goalIdStr := c.Param("goalId")
userId, err := strconv.Atoi(userIdStr) userId, err := strconv.Atoi(userIdStr)
if err != nil { if err != nil {
@ -157,10 +157,10 @@ func deleteUserAllowance(c *gin.Context) {
return return
} }
allowanceId, err := strconv.Atoi(allowanceIdStr) goalId, err := strconv.Atoi(goalIdStr)
if err != nil { if err != nil {
log.Printf("Invalid allowance ID: %v", err) log.Printf("Invalid goal ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid allowance ID"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
return return
} }
@ -175,18 +175,18 @@ func deleteUserAllowance(c *gin.Context) {
return return
} }
err = db.DeleteAllowance(userId, allowanceId) err = db.DeleteGoal(userId, goalId)
if err != nil { if err != nil {
if err.Error() == "allowance not found" { if err.Error() == "goal not found" {
c.JSON(http.StatusNotFound, gin.H{"error": "History not found"}) c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
} else { } else {
log.Printf("Error deleting allowance: %v", err) log.Printf("Error deleting goal: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError}) c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
} }
return return
} }
c.IndentedJSON(http.StatusOK, gin.H{"message": "History deleted successfully"}) c.IndentedJSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
} }
func createTask(c *gin.Context) { func createTask(c *gin.Context) {
@ -290,7 +290,7 @@ func putTask(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"}) c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
} }
func postHistory(c *gin.Context) { func postAllowance(c *gin.Context) {
userIdStr := c.Param("userId") userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr) userId, err := strconv.Atoi(userIdStr)
if err != nil { if err != nil {
@ -299,8 +299,8 @@ func postHistory(c *gin.Context) {
return return
} }
var historyRequest PostHistory var allowanceRequest PostAllowance
if err := c.ShouldBindJSON(&historyRequest); err != nil { if err := c.ShouldBindJSON(&allowanceRequest); err != nil {
log.Printf("Error parsing request body: %v", err) log.Printf("Error parsing request body: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return return
@ -317,13 +317,13 @@ func postHistory(c *gin.Context) {
return return
} }
err = db.AddHistory(userId, &historyRequest) err = db.AddAllowance(userId, &allowanceRequest)
if err != nil { if err != nil {
log.Printf("Error updating history: %v", err) log.Printf("Error updating allowance: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError}) c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return return
} }
c.JSON(http.StatusOK, gin.H{"message": "History updated successfully"}) c.JSON(http.StatusOK, gin.H{"message": "Allowance updated successfully"})
} }
func getHistory(c *gin.Context) { func getHistory(c *gin.Context) {
@ -354,16 +354,14 @@ func start(ctx context.Context, config *ServerConfig) {
defer db.db.MustClose() defer db.db.MustClose()
router := gin.Default() router := gin.Default()
router.Use(cors.New(cors.Config{ router.Use(cors.Default())
AllowOrigins: []string{"*"},
}))
router.GET("/api/users", getUsers) router.GET("/api/users", getUsers)
router.GET("/api/user/:userId", getUser) router.GET("/api/user/:userId", getUser)
router.POST("/api/user/:userId/history", postHistory) router.POST("/api/user/:userId/allowance", postAllowance)
router.GET("/api/user/:userId/history", getHistory) router.GET("/api/user/:userId/history", getHistory)
router.GET("/api/user/:userId/allowance", getUserAllowance) router.GET("/api/user/:userId/goals", getUserGoals)
router.POST("/api/user/:userId/allowance", createUserAllowance) router.POST("/api/user/:userId/goals", createUserGoal)
router.DELETE("/api/user/:userId/allowance/:allowanceId", deleteUserAllowance) router.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
router.POST("/api/tasks", createTask) router.POST("/api/tasks", createTask)
router.GET("/api/tasks", getTasks) router.GET("/api/tasks", getTasks)
router.GET("/api/task/:taskId", getTask) router.GET("/api/task/:taskId", getTask)

View File

@ -12,7 +12,7 @@ create table history
amount integer not null amount integer not null
); );
create table allowances create table goals
( (
id integer primary key, id integer primary key,
user_id integer not null, user_id integer not null,

View File

@ -59,7 +59,7 @@ paths:
404: 404:
description: The users could not be found. description: The users could not be found.
/user/{userId}/history: /user/{userId}/allowance:
get: get:
summary: Gets the allowance history of a user summary: Gets the allowance history of a user
parameters: parameters:
@ -114,7 +114,7 @@ paths:
400: 400:
description: The allowance could not be updated. description: The allowance could not be updated.
/user/{userId}/allowance: /user/{userId}/goals:
get: get:
summary: Gets all goals summary: Gets all goals
parameters: parameters:
@ -201,7 +201,7 @@ paths:
404: 404:
description: The goals could not be found. description: The goals could not be found.
/user/{userId}/allowance/{goalId}: /user/{userId}/goal/{goalId}:
get: get:
summary: Gets information about a goal summary: Gets information about a goal
parameters: parameters:
@ -284,7 +284,7 @@ paths:
404: 404:
description: The goal could not be found. description: The goal could not be found.
/user/{userId}/allowance/{goalId}/complete: /user/{userId}/goal/{goalId}/complete:
post: post:
summary: Completes a goal. summary: Completes a goal.
description: Completes a goal. This will subtract this goal's value from the user's allowance and then remove the goal. description: Completes a goal. This will subtract this goal's value from the user's allowance and then remove the goal.