// package store_ram -- хранилище данных сервиса package store_ram import ( "fmt" "log" "sync" "adt/pkg/alias" "adt/pkg/entities/hotel_room_busy" "adt/pkg/types" ) // StoreRam -- хранилище данных сервиса type StoreRam struct { tableOrder map[alias.OrderId]types.IHotelRoomOrder // таблца заказов бронирования blockOrder sync.RWMutex // блокировка для работы с таблицей заказов бронирования tableRoomBusy map[alias.BusyId]types.IHotelRoomBusy // таблца состояний бронирования номеров blockRoomBusy sync.RWMutex // блокировка для работы с таблицей состояний бронирования номеров } // NewStoreRam -- возвращает новый хранилище данных сервиса func NewStoreRam() *StoreRam { log.Println("NewStoreRam()") return &StoreRam{ tableOrder: map[alias.OrderId]types.IHotelRoomOrder{}, tableRoomBusy: map[alias.BusyId]types.IHotelRoomBusy{ "reddison.lux.2024-01-01": &hotel_room_busy.HotelRoomBusy{ Id_: "reddison.lux.2024-01-01", HotelId_: "reddison", RoomId_: "lux", Date_: "2024-01-01", IsBusy_: true, }, "reddison.lux.2024-01-02": &hotel_room_busy.HotelRoomBusy{ Id_: "reddison.lux.2024-01-02", HotelId_: "reddison", RoomId_: "lux", Date_: "2024-01-02", IsBusy_: true, }, "reddison.lux.2024-01-03": &hotel_room_busy.HotelRoomBusy{ Id_: "reddison.lux.2024-01-03", HotelId_: "reddison", RoomId_: "lux", Date_: "2024-01-03", IsBusy_: true, }, "reddison.lux.2024-01-04": &hotel_room_busy.HotelRoomBusy{ Id_: "reddison.lux.2024-01-04", HotelId_: "reddison", RoomId_: "lux", Date_: "2024-01-04", IsBusy_: true, }, "reddison.lux.2024-01-05": &hotel_room_busy.HotelRoomBusy{ Id_: "reddison.lux.2024-01-05", HotelId_: "reddison", RoomId_: "lux", Date_: "2024-01-05", IsBusy_: false, }, }, } } // SetHotelRoomBusy -- устанавливает состояние бронирования номера func (sf *StoreRam) SetHotelRoomBusy(busy types.IHotelRoomBusy) error { sf.blockRoomBusy.Lock() defer sf.blockRoomBusy.Unlock() if busy == nil { return fmt.Errorf("StoreRam.SetHotelRoomBusy(): IHotelRoomBusy==nil") } // _, isOk := sf.tableRoomBusy[busy.Id()] // if isOk { // return fmt.Errorf("StoreRam.SetHotelRoomBusy(): IHotelRoomBusy with id=%q already exists", busy.Id()) // } sf.tableRoomBusy[busy.Id()] = busy return nil } // GetHotelRoomBusy -- возвращает состояние бронирования номера func (sf *StoreRam) GetHotelRoomBusy(id alias.BusyId) (types.IHotelRoomBusy, error) { sf.blockRoomBusy.RLock() defer sf.blockRoomBusy.RUnlock() busy, isOk := sf.tableRoomBusy[id] if !isOk { return nil, fmt.Errorf("StoreRam.GetHotelRoomBusy(): IHotelRoomBusy with id=%q not found", id) } return busy, nil } // SetOrder -- устанавливает ордер заказа бронирования в хранилище func (sf *StoreRam) SetOrder(order types.IHotelRoomOrder) error { sf.blockOrder.Lock() defer sf.blockOrder.Unlock() if order == nil { return fmt.Errorf("StoreRam.SetOrder(): IHotelRoomOrder==nil") } _, isOk := sf.tableOrder[order.Id()] if isOk { return fmt.Errorf("StoreRam.SetOrder(): IHotelRoomOrder with id %q already exists", order.Id()) } sf.tableOrder[order.Id()] = order return nil } // GetOrder -- возвращает ордер по ID func (sf *StoreRam) GetOrder(id alias.OrderId) (types.IHotelRoomOrder, error) { sf.blockOrder.RLock() defer sf.blockOrder.RUnlock() order, isOk := sf.tableOrder[id] if !isOk { return nil, fmt.Errorf("StoreRam.GetOrder(): IHotelRoomOrder with id=%q not found", id) } return order, nil }