Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func (s *Server) handleContentTypes(c *gin.Context) {
- space, exists := c.Get("space") // set from auth middleware 1: api key can only match to 1: space
- if !exists {
- c.JSON(http.StatusNotFound, apiError(c, "The resource could not be found.", "NotFound"))
- return
- }
- sp := space.(model.Space)
- db := serverServer.DB
- stmt := fmt.Sprintf(`SELECT
- ct.id, ct.name, ct.created_at, ct.updated_at, ct.api_identifier AS APIIdentifier, ct.display_field as DisplayField, ct.description,
- ctfields.field_id "field.id",
- ctfields.name "field.name",
- ft.type "field.type"
- FROM content_types AS ct
- JOIN content_type_field_types ctfields ON ct.id = ctfields.content_type_id
- JOIN field_types ft ON ctfields.field_type_id = ft.id
- WHERE ct.space_id='%v'`, sp.ID)
- results := []model.ContentType{}
- rows, err := db.Query(stmt)
- if err != nil {
- fmt.Println(err)
- }
- for rows.Next() {
- ct := model.ContentType{}
- field := model.Field{}
- err := rows.Scan(&ct.ID, &ct.Name, &ct.CreatedAt, &ct.UpdatedAt, &ct.APIIdentifier, &ct.DisplayField, &ct.Description, &field.ID, &field.Name, &field.Type)
- if err != nil {
- fmt.Println(err)
- }
- if len(results) == 0 || results[len(results)-1].ID != ct.ID {
- ct.Fields = append(ct.Fields, field)
- results = append(results, ct)
- } else {
- results[len(results)-1].Fields = append(results[len(results)-1].Fields, field)
- }
- }
- spew.Dump(results)
- c.JSON(http.StatusOK, contentTypesResponse(results))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement