storage

package
v0.0.0-...-dea8209 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2022 License: Unlicense Imports: 7 Imported by: 0

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

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) AddClass

func (s *SqliteStorage) AddClass(c *models.Class) (string, 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) GetClass

func (s *SqliteStorage) GetClass(ID string) (*models.Class, 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

func NewSqliteStorage(path string) Storage

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL