f92a7cb077820c3bb42cb11e3a06bea2629684f9.svn-base 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/longjoy/micro-go-course/section31/model"
  6. )
  7. var (
  8. ErrClientNotExist = errors.New("clientId is not exist")
  9. ErrClientSecret = errors.New("invalid clientSecret")
  10. )
  11. // Service Define a service interface
  12. type ClientDetailsService interface {
  13. GetClientDetailsByClientId(ctx context.Context, clientId string, clientSecret string)( model.ClientDetails, error)
  14. }
  15. type InMemoryClientDetailsService struct {
  16. clientDetailsDict map[string]*model.ClientDetails
  17. }
  18. func NewInMemoryClientDetailService(clientDetailsList []*model.ClientDetails ) *InMemoryClientDetailsService{
  19. clientDetailsDict := make(map[string]*model.ClientDetails)
  20. if len(clientDetailsList) > 0 {
  21. for _, value := range clientDetailsList {
  22. clientDetailsDict[value.ClientId] = value
  23. }
  24. }
  25. return &InMemoryClientDetailsService{
  26. clientDetailsDict:clientDetailsDict,
  27. }
  28. }
  29. func (service *InMemoryClientDetailsService)GetClientDetailsByClientId(ctx context.Context, clientId string, clientSecret string)(model.ClientDetails, error) {
  30. // 根据 clientId 获取 clientDetails
  31. clientDetails, ok := service.clientDetailsDict[clientId]; if ok{
  32. // 比较 clientSecret 是否正确
  33. if clientDetails.ClientSecret == clientSecret{
  34. return *clientDetails, nil
  35. }else {
  36. return model.ClientDetails{}, ErrClientSecret
  37. }
  38. }else {
  39. return model.ClientDetails{}, ErrClientNotExist
  40. }
  41. }