Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. func (s *Server) handleContentTypes(c *gin.Context) {
  2. space, exists := c.Get("space") // set from auth middleware 1: api key can only match to 1: space
  3. if !exists {
  4. c.JSON(http.StatusNotFound, apiError(c, "The resource could not be found.", "NotFound"))
  5. return
  6. }
  7. sp := space.(model.Space)
  8.  
  9. db := serverServer.DB
  10.  
  11. stmt := fmt.Sprintf(`SELECT
  12. ct.id, ct.name, ct.created_at, ct.updated_at, ct.api_identifier AS APIIdentifier, ct.display_field as DisplayField, ct.description,
  13. ctfields.field_id "field.id",
  14. ctfields.name "field.name",
  15. ft.type "field.type"
  16. FROM content_types AS ct
  17. JOIN content_type_field_types ctfields ON ct.id = ctfields.content_type_id
  18. JOIN field_types ft ON ctfields.field_type_id = ft.id
  19. WHERE ct.space_id='%v'`, sp.ID)
  20.  
  21.  
  22.  
  23. results := []model.ContentType{}
  24.  
  25. rows, err := db.Query(stmt)
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. for rows.Next() {
  30. ct := model.ContentType{}
  31. field := model.Field{}
  32.  
  33. err := rows.Scan(&ct.ID, &ct.Name, &ct.CreatedAt, &ct.UpdatedAt, &ct.APIIdentifier, &ct.DisplayField, &ct.Description, &field.ID, &field.Name, &field.Type)
  34. if err != nil {
  35. fmt.Println(err)
  36. }
  37.  
  38. if len(results) == 0 || results[len(results)-1].ID != ct.ID {
  39. ct.Fields = append(ct.Fields, field)
  40. results = append(results, ct)
  41. } else {
  42. results[len(results)-1].Fields = append(results[len(results)-1].Fields, field)
  43. }
  44. }
  45.  
  46. spew.Dump(results)
  47.  
  48. c.JSON(http.StatusOK, contentTypesResponse(results))
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement