mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools
Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
* GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
* GET /api/v1/users/me/organizations - User organizations
* POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue
API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules
Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
160 lines
4.7 KiB
Go
160 lines
4.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"bugulma/backend/internal/domain"
|
|
"bugulma/backend/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/datatypes"
|
|
)
|
|
|
|
type ResourceFlowHandler struct {
|
|
resourceService *service.ResourceFlowService
|
|
}
|
|
|
|
func NewResourceFlowHandler(resourceService *service.ResourceFlowService) *ResourceFlowHandler {
|
|
return &ResourceFlowHandler{resourceService: resourceService}
|
|
}
|
|
|
|
type CreateResourceFlowRequest struct {
|
|
OrganizationID string `json:"organization_id" binding:"required"`
|
|
SiteID string `json:"site_id" binding:"required"`
|
|
Direction domain.ResourceDirection `json:"direction" binding:"required"`
|
|
Type domain.ResourceType `json:"type" binding:"required"`
|
|
Quality domain.Quality `json:"quality"`
|
|
Quantity domain.Quantity `json:"quantity"`
|
|
TimeProfile domain.TimeProfile `json:"time_profile"`
|
|
EconomicData domain.EconomicData `json:"economic_data"`
|
|
Constraints domain.Constraints `json:"constraints"`
|
|
ServiceDetails domain.ServiceDetails `json:"service_details"`
|
|
PrecisionLevel domain.PrecisionLevel `json:"precision_level"`
|
|
SourceType domain.SourceType `json:"source_type"`
|
|
}
|
|
|
|
func (h *ResourceFlowHandler) Create(c *gin.Context) {
|
|
var req CreateResourceFlowRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
flowReq := service.CreateResourceFlowRequest{
|
|
OrganizationID: req.OrganizationID,
|
|
SiteID: req.SiteID,
|
|
Direction: req.Direction,
|
|
Type: req.Type,
|
|
Quality: req.Quality,
|
|
Quantity: req.Quantity,
|
|
TimeProfile: req.TimeProfile,
|
|
EconomicData: req.EconomicData,
|
|
Constraints: req.Constraints,
|
|
ServiceDetails: req.ServiceDetails,
|
|
PrecisionLevel: req.PrecisionLevel,
|
|
SourceType: req.SourceType,
|
|
}
|
|
|
|
created, err := h.resourceService.Create(c.Request.Context(), flowReq)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, created)
|
|
}
|
|
|
|
func (h *ResourceFlowHandler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var req CreateResourceFlowRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
rf, err := h.resourceService.GetByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Resource flow not found"})
|
|
return
|
|
}
|
|
|
|
// Update fields
|
|
rf.OrganizationID = req.OrganizationID
|
|
rf.SiteID = req.SiteID
|
|
rf.Direction = req.Direction
|
|
rf.Type = req.Type
|
|
|
|
// Marshal JSONB fields
|
|
qualityJSON, _ := json.Marshal(req.Quality)
|
|
quantityJSON, _ := json.Marshal(req.Quantity)
|
|
timeProfileJSON, _ := json.Marshal(req.TimeProfile)
|
|
economicDataJSON, _ := json.Marshal(req.EconomicData)
|
|
constraintsJSON, _ := json.Marshal(req.Constraints)
|
|
serviceDetailsJSON, _ := json.Marshal(req.ServiceDetails)
|
|
|
|
rf.Quality = datatypes.JSON(qualityJSON)
|
|
rf.Quantity = datatypes.JSON(quantityJSON)
|
|
rf.TimeProfile = datatypes.JSON(timeProfileJSON)
|
|
rf.EconomicData = datatypes.JSON(economicDataJSON)
|
|
rf.Constraints = datatypes.JSON(constraintsJSON)
|
|
rf.ServiceDetails = datatypes.JSON(serviceDetailsJSON)
|
|
rf.PrecisionLevel = req.PrecisionLevel
|
|
rf.SourceType = req.SourceType
|
|
|
|
if err := h.resourceService.Update(c.Request.Context(), rf); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, rf)
|
|
}
|
|
|
|
func (h *ResourceFlowHandler) GetByID(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
rf, err := h.resourceService.GetByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Resource flow not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, rf)
|
|
}
|
|
|
|
func (h *ResourceFlowHandler) GetBySite(c *gin.Context) {
|
|
siteID := c.Param("siteId")
|
|
|
|
flows, err := h.resourceService.GetBySiteID(c.Request.Context(), siteID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, flows)
|
|
}
|
|
|
|
func (h *ResourceFlowHandler) GetByOrganization(c *gin.Context) {
|
|
organizationID := c.Param("organizationId")
|
|
|
|
flows, err := h.resourceService.GetByOrganizationID(c.Request.Context(), organizationID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, flows)
|
|
}
|
|
|
|
func (h *ResourceFlowHandler) Delete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
if err := h.resourceService.Delete(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Resource flow not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusNoContent, nil)
|
|
}
|