Add link service
This commit is contained in:
parent
5d1b234b86
commit
d42a9ea53e
1 changed files with 63 additions and 0 deletions
63
link/service.go
Normal file
63
link/service.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue