Add link service

This commit is contained in:
Andrey Chervyakov 2021-03-15 01:23:11 +06:00
parent 5d1b234b86
commit d42a9ea53e

63
link/service.go Normal file
View file

@ -0,0 +1,63 @@
package link
import (
. "cgnolink/err"
)
type Service interface {
Create(link *Link) error
GetById(id string) (*Link, error)
GetAll(limit int, offset int) (Links, error)
DeleteById(id string) error
}
type PgService struct {
rep PgRepository
}
func (s *PgService) Create(link *Link) error {
existingLink, err := s.rep.FindById(link.Id)
if err != nil {
return UnknownError{}
}
if existingLink != nil {
return AlreadyExistsError{Message: "Link with given ID already exists."}
}
if err = s.rep.Save(link); err != nil {
return UnknownError{}
}
return nil
}
func (s *PgService) GetById(id string) (*Link, error) {
link, err := s.rep.FindById(id)
if err != nil {
return nil, UnknownError{}
}
if link == nil {
return nil, NotFoundError{Message: "Link with given ID was not found."}
}
return link, nil
}
func (s *PgService) GetAll(limit int, offset int) (Links, error) {
links, err := s.rep.FindAll(limit, offset)
if err != nil {
return nil, UnknownError{}
}
return links, nil
}
func (s *PgService) DeleteById(id string) error {
if err := s.rep.DeleteById(id); err != nil {
return UnknownError{}
}
return nil
}