sampleapp

package
v0.0.0-...-4125756 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

A Sample App

SampleApp, a demo application that shows how to build and use workspace. It is also using a design guideline to demonstrate how to build a Go application by following few simple rules.

Documentation

Index

Constants

View Source
const (
	// ErrUserNotFound when user is not found.
	ErrUserNotFound = Error("user not found")
	// ErrUserNameMissing when username is missing.
	ErrUserNameMissing = Error("user name is missing")
	// ErrUserAlreadyExist when user already exist in the system.
	ErrUserAlreadyExist = Error("user already exists")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

Error represents a Sampleapp error.

func (Error) Error

func (e Error) Error() string

Error returns the error message.

type User

type User struct {
	ID   string
	Name string
}

User represent a simpleapp User

func (*User) Validate

func (u *User) Validate() error

Validate is a simple helper function that can be uses to validate a user struct data.

type UserService

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

UserService represent the service that manage user without implementation detail, this aim to provide API that will be exposed to the handler (HTTP, GRPC)

This part is really up to the implementation detail and expectation. For instance, we could have this service at the handler level if we plan to only use one api exposure.

func New

func New(storage UserStorage) *UserService

New create a new User service.

func (*UserService) Create

func (u *UserService) Create(ctx context.Context, usr *User) error

Create creates a new user. If the user already exist in the storage, ErrUserAlreadyExist will be returned.

func (*UserService) Delete

func (u *UserService) Delete(ctx context.Context, id string) error

Delete deletes the user form the user storage. If the user does not exist, ErrUserNotFound will be returned.

func (*UserService) Fetch

func (u *UserService) Fetch(ctx context.Context, id string) (*User, error)

Fetch fetches a single user by the given ID.

type UserStorage

type UserStorage interface {
	Fetch(ctx context.Context, id string) (*User, error)
	Create(ctx context.Context, u *User) error
	Delete(ctx context.Context, id string) error
}

UserStorage defines how we store a user. By definition, it is decouple from any backend implementation and its design to be an abstraction of an actual storage.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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