manual.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package strategy
  2. import (
  3. "errors"
  4. "fmt"
  5. "code.osinet.fr/fgm/kurz/storage"
  6. "code.osinet.fr/fgm/kurz/url"
  7. "time"
  8. )
  9. type ManualStrategy struct {
  10. base baseStrategy
  11. }
  12. func (y ManualStrategy) Name() string {
  13. return "manual"
  14. }
  15. /*
  16. Alias() implements AliasingStrategy.Alias().
  17. - options is expected to be a non empty single string
  18. */
  19. func (y ManualStrategy) Alias(long *url.LongUrl, s storage.Storage, options ...interface{}) (url.ShortUrl, error) {
  20. var ret url.ShortUrl
  21. var err error
  22. if len(options) != 1 {
  23. err = errors.New("ManualString.Alias() takes a single optional parameter, which is the requested short URL.")
  24. return ret, err
  25. } else {
  26. requestedAlias, ok := options[0].(string)
  27. if !ok {
  28. err = errors.New(fmt.Sprintf("ManualString.Alias() optional parameter must be a string, got: %+v", requestedAlias))
  29. return ret, err
  30. }
  31. err = nil
  32. /** TODO
  33. * - validate alias is available
  34. */
  35. ret = url.ShortUrl{
  36. Value: requestedAlias,
  37. ShortFor: *long,
  38. Domain: long.Domain(),
  39. Strategy: y.Name(),
  40. SubmittedBy: storage.CurrentUser(),
  41. SubmittedOn: time.Now().UTC().Unix(),
  42. IsEnabled: true,
  43. }
  44. }
  45. return ret, err
  46. }
  47. func (y ManualStrategy) UseCount(s storage.Storage) int {
  48. return y.base.UseCount(s)
  49. }