Skip to content

Commit

Permalink
positions, likely will remove
Browse files Browse the repository at this point in the history
  • Loading branch information
capture120 committed Apr 18, 2024
1 parent 6752968 commit b71cd08
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
34 changes: 34 additions & 0 deletions backend/src/controllers/positions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package controllers

import (
"backend/src/services"
"backend/src/types"
"fmt"
"log"
"net/http"

"github.com/gin-gonic/gin"
)

type PositionsController struct {
positionsService *services.PositionsService
}

func NewPositionsController(positionsService *services.PositionsService) *PositionsController {
return &PositionsController{
positionsService: positionsService,
}
}

func (pc *PositionsController) GetAllPositions(c *gin.Context) {
portfolioID := c.Param("portfolioID")

positions, err := pc.positionsService.GetAllPositions(portfolioID)
if err != nil {
log.Printf("Failed to get positions: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get positions"})
return
}

c.JSON(http.StatusOK, positions)
}
37 changes: 37 additions & 0 deletions backend/src/services/positions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package services

import (
"backend/src/models"

"gorm.io/gorm"
)

type PositionService struct {
DB *gorm.DB
}

func NewPositionService(db *gorm.DB) *PositionService {
return &PositionService{
DB: db,
}
}

func (os *PositionService) CreatePosition(currentUserPortfolioID int, position models.Position) ([]models.Position, error) {
position.UserPortfolioID = currentUserPortfolioID
err := os.DB.Create(&position).Error
if err != nil {
return nil, err
}

return []models.Position{position}, nil
}

func (os *PositionService) GetPositions(currentUserPortfolioID int) ([]models.Position, error) {
var positions []models.Position
err := os.DB.Where("user_portfolio_id = ?", currentUserPortfolioID).Find(&positions).Error
if err != nil {
return nil, err
}

return positions, nil
}

0 comments on commit b71cd08

Please sign in to comment.