Skip to content

Commit

Permalink
Merge pull request #29 from GenerateNU/post-endpoints
Browse files Browse the repository at this point in the history
All missing Post Routes
  • Loading branch information
matherg authored Oct 10, 2023
2 parents 81f5aa4 + 3f70fd2 commit 563a298
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 202 deletions.
67 changes: 40 additions & 27 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package controller
import (
"CaitsCurates/backend/src/model"
"fmt"
"net/http"
"strconv"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"net/http"
)

type Controller interface {
Expand All @@ -22,24 +20,6 @@ func (pg *PgController) Serve() *gin.Engine {
r := gin.Default()
r.Use(cors.Default())

r.GET("/gifts/:id", func(c *gin.Context) {
id := c.Param("id")
intId, err := strconv.Atoi(id)
gift := pg.GetExampleGift(int64(intId))
if err != nil {
panic(err)
}
c.JSON(http.StatusOK, gift)
})

r.GET("/gifts", func(c *gin.Context) {
gifts, err := pg.AllExampleGifts()
if err != nil {
c.JSON(http.StatusInternalServerError, "Oops")
}
c.JSON(http.StatusOK, gifts)
})

// Get incomplete gift requests
r.GET("/requests/incomplete", func(c *gin.Context) {
gifts, err := pg.IncompleteRequests()
Expand All @@ -58,24 +38,57 @@ func (pg *PgController) Serve() *gin.Engine {
c.JSON(http.StatusOK, gifts)
})

r.POST("/addGift", func(c *gin.Context) {
var input model.ExampleGiftInput
fmt.Print(c)
r.POST("/addGiftResponse", func(c *gin.Context) {
var input model.GiftResponse
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, "Failed to unmarshal respone")

fmt.Print(err)

return
}
insertedResponse, err := pg.AddResponse(input)

if err != nil {
c.JSON(http.StatusBadRequest, input)
panic(err)
}

c.JSON(http.StatusOK, insertedResponse)
})
r.POST("/addGiftRequest", func(c *gin.Context) {
var input model.GiftRequest
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, "Failed to unmarshal request")
fmt.Print(err)
return
}
insertedRequest, err := pg.AddRequest(input)

if err != nil {
c.JSON(http.StatusBadRequest, input)
panic(err)
}

c.JSON(http.StatusOK, insertedRequest)
})
r.POST("/addGiftCollection", func(c *gin.Context) {
var input model.GiftCollection
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, "Failed to unmarshal gift")
c.JSON(http.StatusBadRequest, "Failed to unmarshal collection")

fmt.Print(err)

return
}
insertedGift, err := pg.AddExampleGift(input)
insertedCollection, err := pg.AddCollection(input)

if err != nil {
c.JSON(http.StatusBadRequest, input)
panic(err)
}

c.JSON(http.StatusOK, insertedGift)
c.JSON(http.StatusOK, insertedCollection)
})

return r
Expand Down
11 changes: 9 additions & 2 deletions api/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ func main() {
os.Exit(1)
}
// Auto migrate tables
err = db.AutoMigrate(&model.ExampleGift{})

err = db.AutoMigrate(model.User{}, model.Customer{}, model.GiftRequest{}, model.GiftCollection{}, model.GiftResponse{}, model.Admin{})
user := model.User{}
err = db.Create(&user).Error
var retrievedUser model.User
err = db.First(&retrievedUser).Error
customer := model.Customer{
User: retrievedUser,
}
err = db.Create(&customer).Error
// Check for errors
if err != nil {
fmt.Println("Error auto-migrating:", err)
Expand Down
33 changes: 17 additions & 16 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,42 @@ type PgModel struct {
}

type Model interface {
GetExampleGift(int64) ExampleGift
AllExampleGifts() ([]ExampleGift, error)
AddExampleGift(ExampleGiftInput) (ExampleGift, error)
AddRequest(GiftRequest) (GiftRequest, error)
AddResponse(GiftResponse) (GiftResponse, error)
AddCollection(GiftCollection) (GiftCollection, error)
IncompleteRequests() ([]GiftRequest, error)
CompleteRequests() ([]GiftRequest, error)
}

func (m *PgModel) GetExampleGift(id int64) ExampleGift {
gift, err := GetExampleGiftFromDB(m.Conn, id)
func (m *PgModel) AddRequest(inputRequest GiftRequest) (GiftRequest, error) {

createdRequest, err := WriteRequestToDb(m.Conn, inputRequest)

if err != nil {
panic(err)
return GiftRequest{}, err
}

return gift
return createdRequest, nil
}
func (m *PgModel) AddResponse(inputResponse GiftResponse) (GiftResponse, error) {

func (m *PgModel) AddExampleGift(inputGift ExampleGiftInput) (ExampleGift, error) {

createdGift, err := WriteExampleGiftToDb(m.Conn, inputGift)
createdResponse, err := WriteResponseToDb(m.Conn, inputResponse)

if err != nil {
return ExampleGift{}, err
return GiftResponse{}, err
}

return createdGift, nil
return createdResponse, nil
}
func (m *PgModel) AddCollection(inputCollection GiftCollection) (GiftCollection, error) {

func (m *PgModel) AllExampleGifts() ([]ExampleGift, error) {
gifts, err := GetAllExampleGiftsFromDB(m.Conn)
createdCollection, err := WriteCollectionToDb(m.Conn, inputCollection)

if err != nil {
return []ExampleGift{}, err
return GiftCollection{}, err
}
return gifts, nil

return createdCollection, nil
}

func (m *PgModel) IncompleteRequests() ([]GiftRequest, error) {
Expand Down
34 changes: 13 additions & 21 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,23 @@ import (
"gorm.io/gorm"
)

// WriteExampleGiftToDb saves the ExampleGift and returns it
func WriteExampleGiftToDb(db *gorm.DB, inputGift ExampleGiftInput) (ExampleGift, error) {
eg := ExampleGift{Name: inputGift.Name, Price: inputGift.Price}
if err := db.Create(&eg).Error; err != nil {
return ExampleGift{}, err
func WriteRequestToDb(db *gorm.DB, inputRequest GiftRequest) (GiftRequest, error) {
if err := db.Create(&inputRequest).Error; err != nil {
return GiftRequest{}, err
}
return eg, nil
return inputRequest, nil
}

// GetExampleGiftFromDB fetches an ExampleGift by ID
func GetExampleGiftFromDB(db *gorm.DB, id int64) (ExampleGift, error) {
var eg ExampleGift
if err := db.Where("id = ?", id).First(&eg).Error; err != nil {
return ExampleGift{}, err
func WriteResponseToDb(db *gorm.DB, inputResponse GiftResponse) (GiftResponse, error) {
if err := db.Create(&inputResponse).Error; err != nil {
return GiftResponse{}, err
}
return eg, nil
return inputResponse, nil
}

// GetAllExampleGiftsFromDB fetches all ExampleGift
func GetAllExampleGiftsFromDB(db *gorm.DB) ([]ExampleGift, error) {
var gifts []ExampleGift
if err := db.Find(&gifts).Error; err != nil {
return nil, err
func WriteCollectionToDb(db *gorm.DB, inputCollection GiftCollection) (GiftCollection, error) {
if err := db.Create(&inputCollection).Error; err != nil {
return GiftCollection{}, err
}
return gifts, nil
return inputCollection, nil
}

func GetIncompleteGiftRequestsFromDB(db *gorm.DB) ([]GiftRequest, error) {
Expand All @@ -45,4 +37,4 @@ func GetCompleteGiftRequestsFromDB(db *gorm.DB) ([]GiftRequest, error) {
return nil, err
}
return requests, nil
}
}
13 changes: 1 addition & 12 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ import (
"time"
)

type ExampleGift struct {
gorm.Model
Name string
Price int
}

type ExampleGiftInput struct {
Name string
Price int
}

type Gift struct {
gorm.Model
Name string
Expand All @@ -37,7 +26,7 @@ type GiftRequest struct {
RecipientInterests pq.StringArray `gorm:"type:text[]"`
BudgetMax uint
BudgetMin uint
GiftResponse GiftResponse
GiftResponse *GiftResponse
DateNeeded time.Time
}

Expand Down
Loading

0 comments on commit 563a298

Please sign in to comment.