Retrieving proposals.

* Added GET /data/proposals endpoint which returns all submitted enhancement proposals.

Fix #2
This commit is contained in:
zegkljan 2022-09-15 21:03:17 +02:00
parent a5d98c2eb7
commit c1cdd4f904
4 changed files with 33 additions and 1 deletions

View File

@ -95,7 +95,7 @@ type PhotoMetadata struct {
}
type Proposal struct {
OwnerID int `json:"owner_id"`
OwnerID UserID `json:"owner_id"`
Description string `json:"description"`
How string `json:"how"`
}

View File

@ -12,6 +12,7 @@ const (
URIDataPeople = "/data/people"
URIDataFeatures = "/data/features"
URIDataFeaturesPhoto = "/data/features/:feature/photos/:photo"
URIDataProposals = "/data/proposals"
URITileserverRoot = "/tileserver"
URITileserver = URITileserverRoot + "/*x"
URITileTemplate = URITileserverRoot + "/map/tiles/{z}/{x}/{y}.pbf"

View File

@ -105,6 +105,7 @@ func (s *Server) setupRouter() *gin.Engine {
router.GET(URIDataPeople, s.handleGETDataPeople)
router.GET(URIDataFeatures, s.handleGETDataFeatures)
router.GET(URIDataFeaturesPhoto, s.handleGETDataFeaturesPhoto)
router.GET(URIDataProposals, s.handleGETDataProposals)
// tileserver
router.GET(URITileserver, gin.WrapH(s.tileserverSvSet.Handler()))
@ -354,3 +355,12 @@ func (s *Server) handleGETDataFeaturesPhoto(gc *gin.Context) {
gc.Data(http.StatusOK, contentType, photoBytes)
}
func (s *Server) handleGETDataProposals(gc *gin.Context) {
proposals, err := s.getProposals(nil)
if err != nil {
internalError(gc, err)
return
}
gc.JSON(http.StatusOK, proposals)
}

View File

@ -971,3 +971,24 @@ func (s *Server) addProposals(conn *sqlite.Conn, proposals []models.Proposal) er
s.requestCheckpoint()
return nil
}
func (s *Server) getProposals(conn *sqlite.Conn) ([]models.Proposal, error) {
if conn == nil {
conn = s.getDbConn()
defer s.dbpool.Put(conn)
}
proposals := make([]models.Proposal, 0, 100)
err := sqlitex.Exec(conn, "select owner_id, description, how from proposals", func(stmt *sqlite.Stmt) error {
proposals = append(proposals, models.Proposal{
OwnerID: models.UserID(stmt.ColumnInt(0)),
Description: stmt.ColumnText(1),
How: stmt.ColumnText(2),
})
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to get proposals from db: %w", err)
}
return proposals, nil
}