Add paste service with ability to create paste

master
Nick Krichevsky 2019-03-09 22:27:07 -05:00
parent 6b58d28547
commit dddd9bbe8e
3 changed files with 90 additions and 0 deletions

29
handler/paste_service.go Normal file
View File

@ -0,0 +1,29 @@
package handler
import (
"github.com/ollien/updown/repository"
)
// PasteService is a service to produce pastes
type PasteService struct {
db repository.PasteRepository
pasteDir string
}
// NewPasteService makes a new PasteService
func NewPasteService() *PasteService {
return initPasteService()
}
// CreatePaste makes a new paste to be stored
func (service PasteService) CreatePaste(title string) (Paster, error) {
paste, err := service.db.PutPaste(title)
if err != nil {
return Paster{}, err
}
return Paster{
Paste: paste,
pasteDir: service.pasteDir,
}, nil
}

34
handler/wire_gen.go Normal file
View File

@ -0,0 +1,34 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package handler
import (
"github.com/ollien/updown/config"
"github.com/ollien/updown/repository"
)
// Injectors from wire_stubs.go:
func initPasteService() *PasteService {
configConfig := config.Get()
databaseConnector := repository.GetConnector()
pasteRepository := providePasteRepository(databaseConnector)
pasteService := providePasteService(configConfig, pasteRepository)
return pasteService
}
// wire_stubs.go:
func providePasteService(appConfig config.Config, repo repository.PasteRepository) *PasteService {
return &PasteService{
db: repo,
pasteDir: appConfig.StoragePath,
}
}
func providePasteRepository(db *repository.DatabaseConnector) repository.PasteRepository {
return repository.PasteRepository(db)
}

27
handler/wire_stubs.go Normal file
View File

@ -0,0 +1,27 @@
//+build wireinject
package handler
import (
"github.com/google/wire"
"github.com/ollien/updown/config"
"github.com/ollien/updown/repository"
)
// initPasteService is generated by wire. Please see wire_gen.go for the generated implementation
func initPasteService() *PasteService {
wire.Build(providePasteService, providePasteRepository, repository.GetConnector, config.Get)
return nil
}
func providePasteService(appConfig config.Config, repo repository.PasteRepository) *PasteService {
return &PasteService{
db: repo,
pasteDir: appConfig.StoragePath,
}
}
func providePasteRepository(db *repository.DatabaseConnector) repository.PasteRepository {
return repository.PasteRepository(db)
}