Add password protected links
This commit is contained in:
parent
2baa74d520
commit
a01f540a29
6 changed files with 67 additions and 28 deletions
|
|
@ -3,11 +3,15 @@ package link
|
|||
import (
|
||||
apperrors "cgnolink/errors"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var linkNotFoundError = apperrors.NotFoundError{Message: "Link with given ID was not found."}
|
||||
|
||||
type Service interface {
|
||||
Create(link *Link) error
|
||||
GetById(id string) (*Link, error)
|
||||
AccessLink(id string, password string) (*url.URL, error)
|
||||
GetAll(limit int, offset int) (Links, error)
|
||||
Update(data *Link) error
|
||||
DeleteById(id string) error
|
||||
|
|
@ -18,6 +22,21 @@ type PgService struct {
|
|||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func (service *PgService) AccessLink(id string, password string) (*url.URL, error) {
|
||||
link, err := service.GetById(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if link.Password != "" {
|
||||
if password == "" || link.Password != password {
|
||||
return nil, linkNotFoundError
|
||||
}
|
||||
}
|
||||
|
||||
return &link.RedirectURL, nil
|
||||
}
|
||||
|
||||
func NewService(rep Repository) Service {
|
||||
return &PgService{
|
||||
rep: rep,
|
||||
|
|
@ -25,8 +44,8 @@ func NewService(rep Repository) Service {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *PgService) Create(link *Link) error {
|
||||
existingLink, err := s.rep.FindById(link.Id)
|
||||
func (service *PgService) Create(link *Link) error {
|
||||
existingLink, err := service.rep.FindById(link.Id)
|
||||
if err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
|
@ -35,36 +54,36 @@ func (s *PgService) Create(link *Link) error {
|
|||
return apperrors.AlreadyExistsError{Message: "Link with given ID already exists."}
|
||||
}
|
||||
|
||||
if err = s.rep.Save(link); err != nil {
|
||||
if err = service.rep.Save(link); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PgService) GetById(id string) (*Link, error) {
|
||||
if v, found := s.cache.Get(id); found {
|
||||
func (service *PgService) GetById(id string) (*Link, error) {
|
||||
if v, found := service.cache.Get(id); found {
|
||||
if link, ok := v.(*Link); ok {
|
||||
return link, nil
|
||||
}
|
||||
}
|
||||
|
||||
link, err := s.rep.FindById(id)
|
||||
link, err := service.rep.FindById(id)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
if link == nil {
|
||||
return nil, apperrors.NotFoundError{Message: "Link with given ID was not found."}
|
||||
return nil, linkNotFoundError
|
||||
}
|
||||
|
||||
s.cache.Set(id, link, cache.DefaultExpiration)
|
||||
service.cache.Set(id, link, cache.DefaultExpiration)
|
||||
|
||||
return link, nil
|
||||
}
|
||||
|
||||
func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
||||
links, err := s.rep.GetAll(limit, offset)
|
||||
func (service *PgService) GetAll(limit int, offset int) (Links, error) {
|
||||
links, err := service.rep.GetAll(limit, offset)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
|
@ -72,20 +91,20 @@ func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
|||
return links, nil
|
||||
}
|
||||
|
||||
func (s *PgService) Update(data *Link) error {
|
||||
if err := s.rep.Update(data); err != nil {
|
||||
func (service *PgService) Update(data *Link) error {
|
||||
if err := service.rep.Update(data); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
service.cache.Delete(data.Id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PgService) DeleteById(id string) error {
|
||||
s.cache.Delete(id)
|
||||
|
||||
if err := s.rep.DeleteById(id); err != nil {
|
||||
func (service *PgService) DeleteById(id string) error {
|
||||
if err := service.rep.DeleteById(id); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
service.cache.Delete(id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue