mongo.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package mongo
  2. import (
  3. "gopkg.in/mgo.v2"
  4. "gopkg.in/mgo.v2/bson"
  5. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  6. )
  7. type cargoRepository struct {
  8. db string
  9. session *mgo.Session
  10. }
  11. func (r *cargoRepository) Store(cargo *shipping.Cargo) (bool, error) {
  12. sess := r.session.Copy()
  13. defer sess.Close()
  14. c := sess.DB(r.db).C("cargo")
  15. _, err := c.Upsert(bson.M{"trackingid": cargo.TrackingID}, bson.M{"$set": cargo})
  16. return true, err
  17. }
  18. func (r *cargoRepository) Find(id shipping.TrackingID) (*shipping.Cargo, error) {
  19. sess := r.session.Copy()
  20. defer sess.Close()
  21. c := sess.DB(r.db).C("cargo")
  22. var result shipping.Cargo
  23. if err := c.Find(bson.M{"trackingid": id}).One(&result); err != nil {
  24. if err == mgo.ErrNotFound {
  25. return nil, shipping.ErrUnknownCargo
  26. }
  27. return nil, err
  28. }
  29. return &result, nil
  30. }
  31. func (r *cargoRepository) FindAll() []*shipping.Cargo {
  32. sess := r.session.Copy()
  33. defer sess.Close()
  34. c := sess.DB(r.db).C("cargo")
  35. var result []*shipping.Cargo
  36. if err := c.Find(bson.M{}).All(&result); err != nil {
  37. return []*shipping.Cargo{}
  38. }
  39. return result
  40. }
  41. // NewCargoRepository returns a new instance of a MongoDB cargo repository.
  42. func NewCargoRepository(db string, session *mgo.Session) (shipping.CargoRepository, error) {
  43. r := &cargoRepository{
  44. db: db,
  45. session: session,
  46. }
  47. index := mgo.Index{
  48. Key: []string{"trackingid"},
  49. Unique: true,
  50. DropDups: true,
  51. Background: true,
  52. Sparse: true,
  53. }
  54. sess := r.session.Copy()
  55. defer sess.Close()
  56. c := sess.DB(r.db).C("cargo")
  57. if err := c.EnsureIndex(index); err != nil {
  58. return nil, err
  59. }
  60. return r, nil
  61. }
  62. type locationRepository struct {
  63. db string
  64. session *mgo.Session
  65. }
  66. func (r *locationRepository) Find(locode shipping.UNLocode) (*shipping.Location, error) {
  67. sess := r.session.Copy()
  68. defer sess.Close()
  69. c := sess.DB(r.db).C("location")
  70. var result shipping.Location
  71. if err := c.Find(bson.M{"unlocode": locode}).One(&result); err != nil {
  72. if err == mgo.ErrNotFound {
  73. return nil, shipping.ErrUnknownLocation
  74. }
  75. return nil, err
  76. }
  77. return &result, nil
  78. }
  79. func (r *locationRepository) FindAll() []*shipping.Location {
  80. sess := r.session.Copy()
  81. defer sess.Close()
  82. c := sess.DB(r.db).C("location")
  83. var result []*shipping.Location
  84. if err := c.Find(bson.M{}).All(&result); err != nil {
  85. return []*shipping.Location{}
  86. }
  87. return result
  88. }
  89. func (r *locationRepository) store(l *shipping.Location) (bool, error) {
  90. sess := r.session.Copy()
  91. defer sess.Close()
  92. c := sess.DB(r.db).C("location")
  93. _, err := c.Upsert(bson.M{"unlocode": l.UNLocode}, bson.M{"$set": l})
  94. return true, err
  95. }
  96. // NewLocationRepository returns a new instance of a MongoDB location repository.
  97. func NewLocationRepository(db string, session *mgo.Session) (shipping.LocationRepository, error) {
  98. r := &locationRepository{
  99. db: db,
  100. session: session,
  101. }
  102. sess := r.session.Copy()
  103. defer sess.Close()
  104. c := sess.DB(r.db).C("location")
  105. index := mgo.Index{
  106. Key: []string{"unlocode"},
  107. Unique: true,
  108. DropDups: true,
  109. Background: true,
  110. Sparse: true,
  111. }
  112. if err := c.EnsureIndex(index); err != nil {
  113. return nil, err
  114. }
  115. initial := []*shipping.Location{
  116. shipping.Stockholm,
  117. shipping.Melbourne,
  118. shipping.Hongkong,
  119. shipping.Tokyo,
  120. shipping.Rotterdam,
  121. shipping.Hamburg,
  122. }
  123. for _, l := range initial {
  124. r.store(l)
  125. }
  126. return r, nil
  127. }
  128. type voyageRepository struct {
  129. db string
  130. session *mgo.Session
  131. }
  132. func (r *voyageRepository) Find(voyageNumber shipping.VoyageNumber) (*shipping.Voyage, error) {
  133. sess := r.session.Copy()
  134. defer sess.Close()
  135. c := sess.DB(r.db).C("voyage")
  136. var result shipping.Voyage
  137. if err := c.Find(bson.M{"number": voyageNumber}).One(&result); err != nil {
  138. if err == mgo.ErrNotFound {
  139. return nil, shipping.ErrUnknownVoyage
  140. }
  141. return nil, err
  142. }
  143. return &result, nil
  144. }
  145. func (r *voyageRepository) store(v *shipping.Voyage) (bool, error) {
  146. sess := r.session.Copy()
  147. defer sess.Close()
  148. c := sess.DB(r.db).C("voyage")
  149. _, err := c.Upsert(bson.M{"number": v.VoyageNumber}, bson.M{"$set": v})
  150. return true, err
  151. }
  152. // NewVoyageRepository returns a new instance of a MongoDB voyage repository.
  153. func NewVoyageRepository(db string, session *mgo.Session) (shipping.VoyageRepository, error) {
  154. r := &voyageRepository{
  155. db: db,
  156. session: session,
  157. }
  158. sess := r.session.Copy()
  159. defer sess.Close()
  160. c := sess.DB(r.db).C("voyage")
  161. index := mgo.Index{
  162. Key: []string{"number"},
  163. Unique: true,
  164. DropDups: true,
  165. Background: true,
  166. Sparse: true,
  167. }
  168. if err := c.EnsureIndex(index); err != nil {
  169. return nil, err
  170. }
  171. initial := []*shipping.Voyage{
  172. shipping.V100,
  173. shipping.V300,
  174. shipping.V400,
  175. shipping.V0100S,
  176. shipping.V0200T,
  177. shipping.V0300A,
  178. shipping.V0301S,
  179. shipping.V0400S,
  180. }
  181. for _, v := range initial {
  182. r.store(v)
  183. }
  184. return r, nil
  185. }
  186. type handlingEventRepository struct {
  187. db string
  188. session *mgo.Session
  189. }
  190. func (r *handlingEventRepository) Store(e shipping.HandlingEvent) {
  191. sess := r.session.Copy()
  192. defer sess.Close()
  193. c := sess.DB(r.db).C("handling_event")
  194. _ = c.Insert(e)
  195. }
  196. func (r *handlingEventRepository) QueryHandlingHistory(id shipping.TrackingID) shipping.HandlingHistory {
  197. sess := r.session.Copy()
  198. defer sess.Close()
  199. c := sess.DB(r.db).C("handling_event")
  200. var result []shipping.HandlingEvent
  201. _ = c.Find(bson.M{"trackingid": id}).All(&result)
  202. return shipping.HandlingHistory{HandlingEvents: result}
  203. }
  204. // NewHandlingEventRepository returns a new instance of a MongoDB handling event repository.
  205. func NewHandlingEventRepository(db string, session *mgo.Session) shipping.HandlingEventRepository {
  206. return &handlingEventRepository{
  207. db: db,
  208. session: session,
  209. }
  210. }