Skip to content

Commit

Permalink
style: reformat code with golines and gofumpt
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-baiborodine committed Jun 25, 2024
1 parent 67b71a1 commit 68cf62d
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 73 deletions.
21 changes: 17 additions & 4 deletions internal/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,34 @@ func (a CampgroundsApp) CancelBooking(ctx context.Context, cmd command.CancelBoo
return a.commands.CancelBookingHandler.Handle(ctx, cmd)
}

func (a CampgroundsApp) GetCampsites(ctx context.Context, qry query.GetCampsites) ([]*domain.Campsite, error) {
func (a CampgroundsApp) GetCampsites(
ctx context.Context,
qry query.GetCampsites,
) ([]*domain.Campsite, error) {
return a.queries.GetCampsitesHandler.Handle(ctx, qry)
}

func (a CampgroundsApp) GetBooking(ctx context.Context, qry query.GetBooking) (*domain.Booking, error) {
func (a CampgroundsApp) GetBooking(
ctx context.Context,
qry query.GetBooking,
) (*domain.Booking, error) {
return a.queries.GetBookingHandler.Handle(ctx, qry)
}

func (a CampgroundsApp) GetVacantDates(ctx context.Context, qry query.GetVacantDates) ([]string, error) {
func (a CampgroundsApp) GetVacantDates(
ctx context.Context,
qry query.GetVacantDates,
) ([]string, error) {
return a.queries.GetVacantDatesHandler.Handle(ctx, qry)
}

var _ App = (*CampgroundsApp)(nil)

func New(campsites domain.CampsiteRepository, bookings domain.BookingRepository, logger *slog.Logger) *CampgroundsApp {
func New(
campsites domain.CampsiteRepository,
bookings domain.BookingRepository,
logger *slog.Logger,
) *CampgroundsApp {
return &CampgroundsApp{
commands: commands{
CreateCampsiteHandler: command.NewCreateCampsiteHandler(campsites, logger),
Expand Down
5 changes: 4 additions & 1 deletion internal/application/command/cancel_booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ type (
}
)

func NewCancelBookingHandler(bookings domain.BookingRepository, logger *slog.Logger) CancelBookingHandler {
func NewCancelBookingHandler(
bookings domain.BookingRepository,
logger *slog.Logger,
) CancelBookingHandler {
return decorator.ApplyCommandDecorator[CancelBooking](
cancelBookingHandler{bookings: bookings},
logger,
Expand Down
5 changes: 4 additions & 1 deletion internal/application/command/create_booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ type (
}
)

func NewCreateBookingHandler(bookings domain.BookingRepository, logger *slog.Logger) CreateBookingHandler {
func NewCreateBookingHandler(
bookings domain.BookingRepository,
logger *slog.Logger,
) CreateBookingHandler {
return decorator.ApplyCommandDecorator[CreateBooking](
createBookingHandler{
bookings: bookings,
Expand Down
5 changes: 4 additions & 1 deletion internal/application/command/create_campsite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type (
}
)

func NewCreateCampsiteHandler(campsites domain.CampsiteRepository, logger *slog.Logger) CreateCampsiteHandler {
func NewCreateCampsiteHandler(
campsites domain.CampsiteRepository,
logger *slog.Logger,
) CreateCampsiteHandler {
return decorator.ApplyCommandDecorator[CreateCampsite](
createCampsiteHandler{campsites: campsites},
logger,
Expand Down
5 changes: 4 additions & 1 deletion internal/application/command/update_booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ type (
}
)

func NewUpdateBookingHandler(bookings domain.BookingRepository, logger *slog.Logger) UpdateBookingHandler {
func NewUpdateBookingHandler(
bookings domain.BookingRepository,
logger *slog.Logger,
) UpdateBookingHandler {
return decorator.ApplyCommandDecorator[UpdateBooking](
updateBookingHandler{
bookings: bookings,
Expand Down
10 changes: 8 additions & 2 deletions internal/application/decorator/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type loggingCommandHandler[C any] struct {
logger *slog.Logger
}

func ApplyCommandDecorator[C any](handler handler.Command[C], logger *slog.Logger) handler.Command[C] {
func ApplyCommandDecorator[C any](
handler handler.Command[C],
logger *slog.Logger,
) handler.Command[C] {
return loggingCommandHandler[C]{
base: handler,
logger: logger,
Expand Down Expand Up @@ -42,7 +45,10 @@ type loggingQueryHandler[C any, R any] struct {
logger *slog.Logger
}

func ApplyQueryDecorator[C any, R any](handler handler.Query[C, R], logger *slog.Logger) handler.Query[C, R] {
func ApplyQueryDecorator[C any, R any](
handler handler.Query[C, R],
logger *slog.Logger,
) handler.Query[C, R] {
return loggingQueryHandler[C, R]{
base: handler,
logger: logger,
Expand Down
5 changes: 4 additions & 1 deletion internal/application/query/get_booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ type (
}
)

func NewGetBookingHandler(bookings domain.BookingRepository, logger *slog.Logger) GetBookingHandler {
func NewGetBookingHandler(
bookings domain.BookingRepository,
logger *slog.Logger,
) GetBookingHandler {
return decorator.ApplyQueryDecorator[GetBooking, *domain.Booking](
getBookingHandler{bookings: bookings},
logger,
Expand Down
10 changes: 8 additions & 2 deletions internal/application/query/get_campsites.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ type (
}
)

func NewGetCampsitesHandler(campsites domain.CampsiteRepository, logger *slog.Logger) GetCampsitesHandler {
func NewGetCampsitesHandler(
campsites domain.CampsiteRepository,
logger *slog.Logger,
) GetCampsitesHandler {
return decorator.ApplyQueryDecorator[GetCampsites, []*domain.Campsite](
getCampsitesHandler{campsites: campsites},
logger,
)
}

func (h getCampsitesHandler) Handle(ctx context.Context, _ GetCampsites) ([]*domain.Campsite, error) {
func (h getCampsitesHandler) Handle(
ctx context.Context,
_ GetCampsites,
) ([]*domain.Campsite, error) {
return h.campsites.FindAll(ctx)
}
5 changes: 4 additions & 1 deletion internal/application/query/get_vacant_dates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ type (
}
)

func NewGetVacantDatesHandler(bookings domain.BookingRepository, logger *slog.Logger) GetVacantDatesHandler {
func NewGetVacantDatesHandler(
bookings domain.BookingRepository,
logger *slog.Logger,
) GetVacantDatesHandler {
return decorator.ApplyQueryDecorator[GetVacantDates, []string](
getVacantDatesHandler{bookings: bookings},
logger,
Expand Down
15 changes: 13 additions & 2 deletions internal/application/query/get_vacant_dates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func TestGetVacantDates(t *testing.T) {
"FindForDateRange", context.Background(), campsiteID,
parseDateStr(t, "2006-01-02"), parseDateStr(t, "2006-01-03"),
).Return([]*domain.Booking{
{StartDate: parseDateStr(t, "2006-01-02"), EndDate: parseDateStr(t, "2006-01-03")}}, nil)
{
StartDate: parseDateStr(t, "2006-01-02"),
EndDate: parseDateStr(t, "2006-01-03"),
},
}, nil)
},
want: nil,
wantErr: "",
Expand Down Expand Up @@ -139,7 +143,14 @@ func TestGetVacantDates(t *testing.T) {
vacantDates, err := h.Handle(tc.args.ctx, tc.args.qry)
// then
if tc.wantErr != "" {
assert.Containsf(t, err.Error(), tc.wantErr, "GetVacantDates() error = %v, wantErr %v", err, tc.wantErr)
assert.Containsf(
t,
err.Error(),
tc.wantErr,
"GetVacantDates() error = %v, wantErr %v",
err,
tc.wantErr,
)
return
}
assert.Equal(t, tc.want, vacantDates)
Expand Down
17 changes: 12 additions & 5 deletions internal/application/validator/booking_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestBookingAllowedStartDateValidator_Validate(t *testing.T) {
now := bootstrap.AsStartOfDayUTC(time.Now())

var tests = map[string]struct {
tests := map[string]struct {
booking *domain.Booking
wantErr error
}{
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestBookingAllowedStartDateValidator_Validate(t *testing.T) {
func TestBookingMaximumStayValidator_Validate(t *testing.T) {
now := bootstrap.AsStartOfDayUTC(time.Now())

var tests = map[string]struct {
tests := map[string]struct {
booking *domain.Booking
wantErr error
}{
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestBookingMaximumStayValidator_Validate(t *testing.T) {
func TestBookingStartDateBeforeEndDateValidator_Validate(t *testing.T) {
now := bootstrap.AsStartOfDayUTC(time.Now())

var tests = map[string]struct {
tests := map[string]struct {
booking *domain.Booking
wantErr error
}{
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestBookingStartDateBeforeEndDateValidator_Validate(t *testing.T) {
func TestApply(t *testing.T) {
now := bootstrap.AsStartOfDayUTC(time.Now())

var tests = map[string]struct {
tests := map[string]struct {
booking *domain.Booking
wantErrs []string
}{
Expand Down Expand Up @@ -207,7 +207,14 @@ func TestApply(t *testing.T) {
// then
if tc.wantErrs != nil {
for _, wantErr := range tc.wantErrs {
assert.Containsf(t, err.Error(), wantErr, "Apply() error = %v, wantErr %v", err, wantErr)
assert.Containsf(
t,
err.Error(),
wantErr,
"Apply() error = %v, wantErr %v",
err,
wantErr,
)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type (

AppConfig struct {
Environment string
LogLevel string `envconfig:"LOG_LEVEL" default:"DEBUG"`
LogLevel string `envconfig:"LOG_LEVEL" default:"DEBUG"`
PG PGConfig
Rpc RpcConfig
ShutdownTimeout time.Duration `envconfig:"SHUTDOWN_TIMEOUT" default:"30s"`
Expand Down
7 changes: 6 additions & 1 deletion internal/domain/booking_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import (

type BookingRepository interface {
Find(ctx context.Context, bookingID string) (*Booking, error)
FindForDateRange(ctx context.Context, campsiteID string, startDate time.Time, endDate time.Time) ([]*Booking, error)
FindForDateRange(
ctx context.Context,
campsiteID string,
startDate time.Time,
endDate time.Time,
) ([]*Booking, error)
Insert(ctx context.Context, booking *Booking) error
Update(ctx context.Context, booking *Booking) error
}
43 changes: 28 additions & 15 deletions internal/grpc/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,34 @@ const (
)

func logServiceCalls(l *slog.Logger) logging.Logger {
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
f := make(map[string]any, len(fields)/2)
i := logging.Fields(fields).Iterator()
return logging.LoggerFunc(
func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
f := make(map[string]any, len(fields)/2)
i := logging.Fields(fields).Iterator()

for i.Next() {
k, v := i.At()
f[k] = v
}
for i.Next() {
k, v := i.At()
f[k] = v
}

if strings.Contains(msg, "finished call") {
l.Log(ctx, slog.Level(lvl), msg, grpcService, f[grpcService], grpcMethod, f[grpcMethod],
grpcTimeMs, f[grpcTimeMs], grpcCode, f[grpcCode])
} else {
l.Log(ctx, slog.Level(lvl), msg, grpcService, f[grpcService], grpcMethod, f[grpcMethod],
grpcTimeMs, f[grpcTimeMs])
}
})
if strings.Contains(msg, "finished call") {
l.Log(
ctx,
slog.Level(lvl),
msg,
grpcService,
f[grpcService],
grpcMethod,
f[grpcMethod],
grpcTimeMs,
f[grpcTimeMs],
grpcCode,
f[grpcCode],
)
} else {
l.Log(ctx, slog.Level(lvl), msg, grpcService, f[grpcService], grpcMethod, f[grpcMethod],
grpcTimeMs, f[grpcTimeMs])
}
},
)
}
35 changes: 28 additions & 7 deletions internal/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func RegisterServer(app application.App, registrar grpc.ServiceRegistrar) error
return nil
}

func (s server) GetCampsites(ctx context.Context, _ *api.GetCampsitesRequest) (*api.GetCampsitesResponse, error) {
func (s server) GetCampsites(
ctx context.Context,
_ *api.GetCampsitesRequest,
) (*api.GetCampsitesResponse, error) {
campsites, err := s.app.GetCampsites(ctx, query.GetCampsites{})
if err != nil {
return nil, err
Expand All @@ -61,7 +64,10 @@ func (s server) GetCampsites(ctx context.Context, _ *api.GetCampsitesRequest) (*
}, nil
}

func (s server) CreateCampsite(ctx context.Context, req *api.CreateCampsiteRequest) (*api.CreateCampsiteResponse, error) {
func (s server) CreateCampsite(
ctx context.Context,
req *api.CreateCampsiteRequest,
) (*api.CreateCampsiteResponse, error) {
campsite := command.CreateCampsite{
CampsiteID: uuid.New().String(),
CampsiteCode: req.CampsiteCode,
Expand All @@ -81,7 +87,10 @@ func (s server) CreateCampsite(ctx context.Context, req *api.CreateCampsiteReque
}, nil
}

func (s server) GetBooking(ctx context.Context, req *api.GetBookingRequest) (*api.GetBookingResponse, error) {
func (s server) GetBooking(
ctx context.Context,
req *api.GetBookingRequest,
) (*api.GetBookingResponse, error) {
booking, err := s.app.GetBooking(ctx, query.GetBooking{BookingID: req.BookingId})
if err != nil {
return nil, handleDomainError(err)
Expand All @@ -92,7 +101,10 @@ func (s server) GetBooking(ctx context.Context, req *api.GetBookingRequest) (*ap
}, nil
}

func (s server) CreateBooking(ctx context.Context, req *api.CreateBookingRequest) (*api.CreateBookingResponse, error) {
func (s server) CreateBooking(
ctx context.Context,
req *api.CreateBookingRequest,
) (*api.CreateBookingResponse, error) {
booking := command.CreateBooking{
BookingID: uuid.New().String(),
CampsiteID: req.CampsiteId,
Expand All @@ -111,7 +123,10 @@ func (s server) CreateBooking(ctx context.Context, req *api.CreateBookingRequest
}, nil
}

func (s server) UpdateBooking(ctx context.Context, req *api.UpdateBookingRequest) (*api.UpdateBookingResponse, error) {
func (s server) UpdateBooking(
ctx context.Context,
req *api.UpdateBookingRequest,
) (*api.UpdateBookingResponse, error) {
booking := command.UpdateBooking{
BookingID: req.Booking.BookingId,
CampsiteID: req.Booking.CampsiteId,
Expand All @@ -127,7 +142,10 @@ func (s server) UpdateBooking(ctx context.Context, req *api.UpdateBookingRequest
return &api.UpdateBookingResponse{}, nil
}

func (s server) CancelBooking(ctx context.Context, req *api.CancelBookingRequest) (*api.CancelBookingResponse, error) {
func (s server) CancelBooking(
ctx context.Context,
req *api.CancelBookingRequest,
) (*api.CancelBookingResponse, error) {
booking := command.CancelBooking{
BookingID: req.GetBookingId(),
}
Expand All @@ -138,7 +156,10 @@ func (s server) CancelBooking(ctx context.Context, req *api.CancelBookingRequest
return &api.CancelBookingResponse{}, nil
}

func (s server) GetVacantDates(ctx context.Context, req *api.GetVacantDatesRequest) (*api.GetVacantDatesResponse, error) {
func (s server) GetVacantDates(
ctx context.Context,
req *api.GetVacantDatesRequest,
) (*api.GetVacantDatesResponse, error) {
vacantDates, err := s.app.GetVacantDates(ctx, query.GetVacantDates{
CampsiteID: req.CampsiteId,
StartDate: req.StartDate,
Expand Down
Loading

0 comments on commit 68cf62d

Please sign in to comment.