Guest User

Untitled

a guest
Nov 19th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. package cmds
  2.  
  3. import (
  4. "context"
  5. "strings"
  6. "time"
  7.  
  8. "github.com/jinzhu/gorm"
  9.  
  10. "github.com/opensimsim/api-v3/errors"
  11. "github.com/opensimsim/api-v3/kernel"
  12. )
  13.  
  14. // GetAllHolidayGroupsCmd is used to fetch all of the owner's
  15. // registered holiday groups.
  16. type GetAllHolidayGroupsCmd struct {
  17. ownerID string
  18. db kernel.SQLCommon
  19. }
  20.  
  21. // NewGetAllHolidayGroupsCmd - Creates a new GetAllHolidayGroupsCmd struct
  22. func NewGetAllHolidayGroupsCmd(db kernel.SQLCommon, ownerID string) *GetAllHolidayGroupsCmd {
  23. s := &GetAllHolidayGroupsCmd{
  24. db: db,
  25. ownerID: ownerID,
  26. }
  27. return s
  28. }
  29.  
  30. type summaryResult struct {
  31. ID string `gorm:"column:id;" json:"id"`
  32. Name string `gorm:"column:name;" json:"name"`
  33. GooglePlaceID string `gorm:"column:goo_place_id;" json:"google_place_id"`
  34. }
  35.  
  36. // Handle - is the command handler for GetAllHolidayGroupsCmd
  37. func (s *GetAllHolidayGroupsCmd) Handle(ctx context.Context) (interface{}, error) {
  38.  
  39. gormDB, err := gorm.Open("mysql", s.db)
  40. if err != nil {
  41. return nil, err
  42. }
  43.  
  44. results := []summaryResult{}
  45.  
  46. gormDB = gormDB.
  47. Table("holiday_groups").
  48. Where("company_id = ?", s.ownerID).
  49. Order("id")
  50.  
  51. if err := gormDB.Find(&results).Error; err != nil {
  52. return nil, err
  53. }
  54.  
  55. shell := map[string]interface{}{
  56. "holidays": results,
  57. }
  58.  
  59. return shell, nil
  60. }
  61.  
  62. // GetDetailedHolidayGroupCmd is used to fetch detailed information of a specific holiday group
  63. type GetDetailedHolidayGroupCmd struct {
  64. groupID string
  65. db kernel.SQLCommon
  66. }
  67.  
  68. // NewGetDetailedHolidayGroupCmd - Creates struct
  69. func NewGetDetailedHolidayGroupCmd(db kernel.SQLCommon, groupID string) *GetDetailedHolidayGroupCmd {
  70. s := &GetDetailedHolidayGroupCmd{
  71. db: db,
  72. groupID: groupID,
  73. }
  74. return s
  75. }
  76.  
  77. type rawResult struct {
  78. GroupID string `gorm:"column:group_id;"`
  79. GroupName string `gorm:"column:group_name;"`
  80. GooglePlaceID string `gorm:"column:goo_place_id;"`
  81. LocationID string `gorm:"column:location_id;"`
  82. LocationName string `gorm:"column:location_name;"`
  83. AvatarURL string `gorm:"column:avatar_url;"`
  84. Name string `gorm:"column:name;"`
  85. CreatedBy *string `gorm:"column:created_by;"`
  86. Code string `gorm:"column:code;"`
  87. City *string `gorm:"column:city;"`
  88. Region *string `gorm:"column:region;"`
  89. Country string `gorm:"column:country;"`
  90. StartDate time.Time `gorm:"column:start_date;"`
  91. EndDate time.Time `gorm:"column:end_date;"`
  92. }
  93.  
  94. // Handle - is the command handler for GetDetailedHolidayGroupCmd
  95. func (s *GetDetailedHolidayGroupCmd) Handle(ctx context.Context) (interface{}, error) {
  96.  
  97. gormDB, err := gorm.Open("mysql", s.db)
  98. if err != nil {
  99. return nil, err
  100. }
  101.  
  102. var rawResults []rawResult
  103.  
  104. gormDB = gormDB.Table("holiday_groups").
  105. Select("holiday_groups.id as group_id, holiday_groups.name as group_name, holiday_groups.goo_place_id, entities.id as location_id, entities.name as location_name, entities.avatar_url, holidays.*").
  106. Joins("INNER JOIN holiday_group_locations ON holiday_groups.id = holiday_group_locations.group_id").
  107. Joins("INNER JOIN holiday_group_codes ON holiday_group_codes.group_id = holiday_groups.id").
  108. Joins("INNER JOIN holidays ON holiday_group_codes.holiday_code = holidays.code").
  109. Joins("INNER JOIN entities ON entities.id = holiday_group_locations.location_id").
  110. Where("holiday_groups.id = ?", s.groupID)
  111. if err := gormDB.Find(&rawResults).Error; err != nil {
  112. return nil, err
  113. }
  114.  
  115. if len(rawResults) == 0 {
  116. return nil, errors.NewNotFoundError("holiday_group")
  117. }
  118.  
  119. //Process results
  120. var (
  121. groupID string
  122. groupName string
  123. groupGooglePlaceID string
  124. )
  125.  
  126. groupID = rawResults[0].GroupID
  127. groupName = rawResults[0].GroupName
  128. groupGooglePlaceID = rawResults[0].GooglePlaceID
  129.  
  130. // We are using a map for testing for uniqueness
  131. _locations := map[string]Location{}
  132. dates := map[string]Holiday{}
  133.  
  134. currentTime := time.Now().UTC()
  135.  
  136. for _, v := range rawResults {
  137. if _, ok := _locations[v.LocationID]; !ok {
  138. _locations[v.LocationID] = Location{
  139. ID: v.LocationID,
  140. Name: v.LocationName,
  141. AvatarURL: v.AvatarURL,
  142. }
  143. }
  144.  
  145. h := Holiday{
  146. Name: v.Name,
  147. Country: v.Country,
  148. Code: v.Code,
  149. Region: v.Region,
  150. City: v.City,
  151. Custom: v.CreatedBy != nil,
  152. StartDate: strings.Replace(v.StartDate.Format("2006-01-02"), "Z", "", 1),
  153. EndDate: strings.Replace(v.EndDate.Add(24*time.Hour).Add(-1*time.Second).Format("2006-01-02"), "Z", "", 1),
  154. start: v.StartDate,
  155. end: v.EndDate,
  156. }
  157.  
  158. // Remove duplicate codes from origResults.
  159. // Try and have at least 1 entry. If more than 1 entry, then always the upcoming holiday for a given code.
  160. storedHoliday, exists := dates[v.Code]
  161. if !exists {
  162. //We are encountering this public holiday for the first time
  163. dates[v.Code] = h
  164. } else {
  165. if !v.StartDate.After(currentTime) && v.StartDate.After(storedHoliday.start) {
  166. //Replace
  167. dates[v.Code] = h
  168. } else if !v.StartDate.Before(currentTime) && v.StartDate.Before(storedHoliday.start) {
  169. //Replace
  170. dates[v.Code] = h
  171. } else {
  172. //Ignore this later day of the same holiday
  173. continue
  174. }
  175. }
  176. }
  177.  
  178. locations := []Location{}
  179. for _, val := range _locations {
  180. locations = append(locations, val)
  181. }
  182.  
  183. shell := map[string]interface{}{
  184. "holiday": map[string]interface{}{
  185. "id": groupID,
  186. "name": groupName,
  187. "google_place_id": groupGooglePlaceID,
  188. "locations": locations,
  189. "days": dates,
  190. },
  191. }
  192.  
  193. return shell, nil
  194. }
Add Comment
Please, Sign In to add comment