store_ram.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // package store_ram -- хранилище данных сервиса
  2. package store_ram
  3. import (
  4. "fmt"
  5. "log"
  6. "sync"
  7. "adt/pkg/alias"
  8. "adt/pkg/entities/hotel_room_busy"
  9. "adt/pkg/types"
  10. )
  11. // StoreRam -- хранилище данных сервиса
  12. type StoreRam struct {
  13. tableOrder map[alias.OrderId]types.IHotelRoomOrder // таблца заказов бронирования
  14. blockOrder sync.RWMutex // блокировка для работы с таблицей заказов бронирования
  15. tableRoomBusy map[alias.BusyId]types.IHotelRoomBusy // таблца состояний бронирования номеров
  16. blockRoomBusy sync.RWMutex // блокировка для работы с таблицей состояний бронирования номеров
  17. }
  18. // NewStoreRam -- возвращает новый хранилище данных сервиса
  19. func NewStoreRam() *StoreRam {
  20. log.Println("NewStoreRam()")
  21. return &StoreRam{
  22. tableOrder: map[alias.OrderId]types.IHotelRoomOrder{},
  23. tableRoomBusy: map[alias.BusyId]types.IHotelRoomBusy{
  24. "reddison.lux.2024-01-01": &hotel_room_busy.HotelRoomBusy{
  25. Id_: "reddison.lux.2024-01-01",
  26. HotelId_: "reddison",
  27. RoomId_: "lux",
  28. Date_: "2024-01-01",
  29. IsBusy_: true,
  30. },
  31. "reddison.lux.2024-01-02": &hotel_room_busy.HotelRoomBusy{
  32. Id_: "reddison.lux.2024-01-02",
  33. HotelId_: "reddison",
  34. RoomId_: "lux",
  35. Date_: "2024-01-02",
  36. IsBusy_: true,
  37. },
  38. "reddison.lux.2024-01-03": &hotel_room_busy.HotelRoomBusy{
  39. Id_: "reddison.lux.2024-01-03",
  40. HotelId_: "reddison",
  41. RoomId_: "lux",
  42. Date_: "2024-01-03",
  43. IsBusy_: true,
  44. },
  45. "reddison.lux.2024-01-04": &hotel_room_busy.HotelRoomBusy{
  46. Id_: "reddison.lux.2024-01-04",
  47. HotelId_: "reddison",
  48. RoomId_: "lux",
  49. Date_: "2024-01-04",
  50. IsBusy_: true,
  51. },
  52. "reddison.lux.2024-01-05": &hotel_room_busy.HotelRoomBusy{
  53. Id_: "reddison.lux.2024-01-05",
  54. HotelId_: "reddison",
  55. RoomId_: "lux",
  56. Date_: "2024-01-05",
  57. IsBusy_: false,
  58. },
  59. },
  60. }
  61. }
  62. // SetHotelRoomBusy -- устанавливает состояние бронирования номера
  63. func (sf *StoreRam) SetHotelRoomBusy(busy types.IHotelRoomBusy) error {
  64. sf.blockRoomBusy.Lock()
  65. defer sf.blockRoomBusy.Unlock()
  66. if busy == nil {
  67. return fmt.Errorf("StoreRam.SetHotelRoomBusy(): IHotelRoomBusy==nil")
  68. }
  69. // _, isOk := sf.tableRoomBusy[busy.Id()]
  70. // if isOk {
  71. // return fmt.Errorf("StoreRam.SetHotelRoomBusy(): IHotelRoomBusy with id=%q already exists", busy.Id())
  72. // }
  73. sf.tableRoomBusy[busy.Id()] = busy
  74. return nil
  75. }
  76. // GetHotelRoomBusy -- возвращает состояние бронирования номера
  77. func (sf *StoreRam) GetHotelRoomBusy(id alias.BusyId) (types.IHotelRoomBusy, error) {
  78. sf.blockRoomBusy.RLock()
  79. defer sf.blockRoomBusy.RUnlock()
  80. busy, isOk := sf.tableRoomBusy[id]
  81. if !isOk {
  82. return nil, fmt.Errorf("StoreRam.GetHotelRoomBusy(): IHotelRoomBusy with id=%q not found", id)
  83. }
  84. return busy, nil
  85. }
  86. // SetOrder -- устанавливает ордер заказа бронирования в хранилище
  87. func (sf *StoreRam) SetOrder(order types.IHotelRoomOrder) error {
  88. sf.blockOrder.Lock()
  89. defer sf.blockOrder.Unlock()
  90. if order == nil {
  91. return fmt.Errorf("StoreRam.SetOrder(): IHotelRoomOrder==nil")
  92. }
  93. _, isOk := sf.tableOrder[order.Id()]
  94. if isOk {
  95. return fmt.Errorf("StoreRam.SetOrder(): IHotelRoomOrder with id %q already exists", order.Id())
  96. }
  97. sf.tableOrder[order.Id()] = order
  98. return nil
  99. }
  100. // GetOrder -- возвращает ордер по ID
  101. func (sf *StoreRam) GetOrder(id alias.OrderId) (types.IHotelRoomOrder, error) {
  102. sf.blockOrder.RLock()
  103. defer sf.blockOrder.RUnlock()
  104. order, isOk := sf.tableOrder[id]
  105. if !isOk {
  106. return nil, fmt.Errorf("StoreRam.GetOrder(): IHotelRoomOrder with id=%q not found", id)
  107. }
  108. return order, nil
  109. }