database

package
v0.0.0-...-a53416d Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2021 License: MIT Imports: 16 Imported by: 0

README

Database adapter

TODO

  1. Explain why separation for bikes and reservations is done this way.
  2. Explain why wy are using sql(x) and write a bit about alternatives.
  3. Explain logger usage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter is a storage adapter for app.

func NewAdapter

func NewAdapter(
	hostport string,
	dbname string,
	user string,
	pass string,
	migrationsDir string,
	log logrus.FieldLogger,
) (*Adapter, error)

NewAdapter creates new db adapter.

func (*Adapter) Bikes

func (a *Adapter) Bikes() *BikesRepository

Bikes returns bikes repository.

func (*Adapter) Close

func (a *Adapter) Close()

Close closes underlying db client.

func (*Adapter) Customers

func (a *Adapter) Customers() *CustomersRepository

Customers returns customers repository.

func (*Adapter) Reservations

func (a *Adapter) Reservations() *ReservationsRepository

Reservations returns reservations repository.

type BikesRepository

type BikesRepository struct {
	// contains filtered or unexported fields
}

BikesRepository manages bikes in db.

func (*BikesRepository) Create

func (r *BikesRepository) Create(ctx context.Context, b bikerental.Bike) error

Create creates new bike in db.

func (*BikesRepository) Delete

func (r *BikesRepository) Delete(ctx context.Context, id string) error

Delete deletes a bike from db by id. If bike is not in db, returns app.ErrNotFound error.

func (*BikesRepository) Get

Get returns a bike by id. If it doesn't exists, returns app.ErrNotFound error.

func (*BikesRepository) List

func (r *BikesRepository) List(ctx context.Context) ([]bikerental.Bike, error)

List returns list of all bikes from db sorted by name ascending.

func (*BikesRepository) Update

func (r *BikesRepository) Update(ctx context.Context, id string, b bikerental.Bike) error

Update updates a bike in db by id. If bike is not in db, returns app.ErrNotFound error.

type CustomersRepository

type CustomersRepository struct {
	// contains filtered or unexported fields
}

CustomersRepository manages customers in db.

func (*CustomersRepository) Create

Create creates new customer in db.

func (*CustomersRepository) CreateInTx

func (r *CustomersRepository) CreateInTx(ctx context.Context, tx *sqlx.Tx, c bikerental.Customer) error

CreateInTx creates new customer in db using existing db transaction.

func (*CustomersRepository) Delete

func (r *CustomersRepository) Delete(ctx context.Context, id string) error

Delete removes customer from db.

func (*CustomersRepository) Get

Get returns a customer by id. If it doesn't exists, returns app.ErrNotFound error.

func (*CustomersRepository) GetInTx

func (r *CustomersRepository) GetInTx(ctx context.Context, tx *sqlx.Tx, id string) (*bikerental.Customer, error)

GetInTx returns a customer by id using existing transaction. If customer doesn't exists, returns app.ErrNotFound error.

type ReservationsRepository

type ReservationsRepository struct {
	// contains filtered or unexported fields
}

ReservationsRepository manages reservation data in db.

func (*ReservationsRepository) Create

Create creates new reservation in db. Bike id must be provided. If customer doesn't exists, it is created with reservation.

func (*ReservationsRepository) Delete

func (r *ReservationsRepository) Delete(ctx context.Context, id string) error

Delete deletes reservation from db.

func (*ReservationsRepository) Get

Get returns a reservation by id. Returns app.ErrNotFound if reservation doesn't exists.

func (*ReservationsRepository) List

List returns list of reservations matching request criteria.

func (*ReservationsRepository) SetStatus

SetStatus updates the status of the reservation by its id. Returns app.ErrNotFound if reservation doesn't exists.

Jump to

Keyboard shortcuts

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