Documentation ¶
Overview ¶
The `storage` package implements the data layer for our API.
The idea behind the design of the package is to make it loosely coupled to the rest of the application, in order to ease further development and refactoring as the requirements evolve.
In order to give a better idea of the design, the exercise provides two different ways of storing data that are transparent to the web service logic serving our API. The application can store data either in memory, using a simple map that will be lost on exit, or in a SQLite database persisted on file.
┌───────────────────┐ ┌───────────────────┐ │ │ │ │ │ Application │─────▶│ Storage interface │ │ │ │ │ └───────────────────┘ └───────────────────┘
▲ ┌─────────────┴─────────────┐ │ │ ┌───────────────────┐ ┌───────────────────┐ │ In-mem │ │ SQLite │ │ implementation │ │ implementation │ └───────────────────┘ └───────────────────┘
Index ¶
- type SqliteStorage
- func (s *SqliteStorage) AddBooking(b *models.Booking) (int, error)
- func (s *SqliteStorage) AddClass(c *models.Class) (string, error)
- func (s *SqliteStorage) Close() error
- func (s *SqliteStorage) DeleteBooking(ID int) error
- func (s *SqliteStorage) DeleteClass(ID string) error
- func (s *SqliteStorage) GetBooking(ID int) (*models.Booking, error)
- func (s *SqliteStorage) GetBookings() ([]*models.Booking, error)
- func (s *SqliteStorage) GetClass(ID string) (*models.Class, error)
- func (s *SqliteStorage) GetClasses() ([]*models.Class, error)
- func (s *SqliteStorage) UpdateBooking(ID int, c *models.Booking) error
- func (s *SqliteStorage) UpdateClass(ID string, c *models.Class) error
- type Storage
- type VolatileStorage
- func (s *VolatileStorage) AddBooking(b *models.Booking) (int, error)
- func (s *VolatileStorage) AddClass(c *models.Class) (string, error)
- func (s *VolatileStorage) Close() error
- func (s *VolatileStorage) DeleteBooking(ID int) error
- func (s *VolatileStorage) DeleteClass(ID string) error
- func (s *VolatileStorage) GetBooking(ID int) (*models.Booking, error)
- func (s *VolatileStorage) GetBookings() ([]*models.Booking, error)
- func (s *VolatileStorage) GetClass(ID string) (*models.Class, error)
- func (s *VolatileStorage) GetClasses() ([]*models.Class, error)
- func (s *VolatileStorage) UpdateBooking(ID int, booking *models.Booking) error
- func (s *VolatileStorage) UpdateClass(ID string, c *models.Class) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SqliteStorage ¶
type SqliteStorage struct {
// contains filtered or unexported fields
}
SqliteStorage implements the Storage interface saving data in a SQLite database on disk.
func (*SqliteStorage) AddBooking ¶
func (s *SqliteStorage) AddBooking(b *models.Booking) (int, error)
func (*SqliteStorage) Close ¶
func (s *SqliteStorage) Close() error
func (*SqliteStorage) DeleteBooking ¶
func (s *SqliteStorage) DeleteBooking(ID int) error
func (*SqliteStorage) DeleteClass ¶
func (s *SqliteStorage) DeleteClass(ID string) error
func (*SqliteStorage) GetBooking ¶
func (s *SqliteStorage) GetBooking(ID int) (*models.Booking, error)
func (*SqliteStorage) GetBookings ¶
func (s *SqliteStorage) GetBookings() ([]*models.Booking, error)
func (*SqliteStorage) GetClasses ¶
func (s *SqliteStorage) GetClasses() ([]*models.Class, error)
func (*SqliteStorage) UpdateBooking ¶
func (s *SqliteStorage) UpdateBooking(ID int, c *models.Booking) error
func (*SqliteStorage) UpdateClass ¶
func (s *SqliteStorage) UpdateClass(ID string, c *models.Class) error
type Storage ¶
type Storage interface { // Class AddClass(*models.Class) (string, error) GetClasses() ([]*models.Class, error) GetClass(ID string) (*models.Class, error) UpdateClass(ID string, class *models.Class) error DeleteClass(ID string) error // Booking AddBooking(*models.Booking) (int, error) GetBookings() ([]*models.Booking, error) GetBooking(ID int) (*models.Booking, error) UpdateBooking(ID int, booking *models.Booking) error DeleteBooking(ID int) error // Others Close() error }
Storage is the public API of our storage system. In this example we provide two concrete implementations of this interface: VolatileStorage and SqliteStorage
func NewSqliteStorage ¶
NewSqliteStorage creates the database on file and loads the initial fixtures. The path to the database file is passed by the caller with the `path` parameter.
func NewVolatileStorage ¶
func NewVolatileStorage() Storage
NewVolatileStorage creates the data in memory and loads the initial fixtures.
type VolatileStorage ¶
type VolatileStorage struct {
// contains filtered or unexported fields
}
VolatileStorage implements a trivial in-memory storage for the Storage interface using maps.
func (*VolatileStorage) AddBooking ¶
func (s *VolatileStorage) AddBooking(b *models.Booking) (int, error)
func (*VolatileStorage) AddClass ¶
func (s *VolatileStorage) AddClass(c *models.Class) (string, error)
func (*VolatileStorage) Close ¶
func (s *VolatileStorage) Close() error
func (*VolatileStorage) DeleteBooking ¶
func (s *VolatileStorage) DeleteBooking(ID int) error
func (*VolatileStorage) DeleteClass ¶
func (s *VolatileStorage) DeleteClass(ID string) error
func (*VolatileStorage) GetBooking ¶
func (s *VolatileStorage) GetBooking(ID int) (*models.Booking, error)
func (*VolatileStorage) GetBookings ¶
func (s *VolatileStorage) GetBookings() ([]*models.Booking, error)
func (*VolatileStorage) GetClass ¶
func (s *VolatileStorage) GetClass(ID string) (*models.Class, error)
func (*VolatileStorage) GetClasses ¶
func (s *VolatileStorage) GetClasses() ([]*models.Class, error)
func (*VolatileStorage) UpdateBooking ¶
func (s *VolatileStorage) UpdateBooking(ID int, booking *models.Booking) error
func (*VolatileStorage) UpdateClass ¶
func (s *VolatileStorage) UpdateClass(ID string, c *models.Class) error