initial commit 2

This commit is contained in:
Senad Uka
2018-04-25 13:16:36 +02:00
parent c1520d169c
commit 99c10b75fb
167 changed files with 25057 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package service
import "bitbucket.org/nemt/nemt-portal-api/domain/entity"
// userService is the domain service for user operations
type notificationService struct {
svc *Service
}
// newUserService returns an instance of userService
func newNotificationService(svc *Service) *notificationService {
return &notificationService{
svc: svc,
}
}
// Save the ride for a expected user
func (s *notificationService) Create(notification entity.Notification) (entity.Notification, error) {
return s.svc.db.Notification().Create(notification)
}
func (c *notificationService) GetByUserUUIDAndReadStatus(userUUID string, status string, isRead bool) ([]entity.Notification, error) {
return c.svc.db.Notification().GetByUserUUIDAndReadStatus(userUUID, status, isRead)
}
func (c *notificationService) GetByUserUUID(userUUID string, status string) ([]entity.Notification, error) {
return c.svc.db.Notification().GetByUserUUID(userUUID, status)
}
func (c *notificationService) ReadStatus(notificationUUID string, isRead bool) error {
return c.svc.db.Notification().ReadStatus(notificationUUID, isRead)
}
func (c *notificationService) GetLastNotificationFromPhoneNumber(notificationType string, phoneNumber string, status string) (entity.Notification, error) {
return c.svc.db.Notification().GetLastNotificationFromPhoneNumber(notificationType, phoneNumber, status)
}

View File

@@ -0,0 +1,113 @@
package service
import (
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)
// userService is the domain service for user operations
type organizationService struct {
svc *Service
}
// newUserService returns an instance of userService
func newOrganizationService(svc *Service) *organizationService {
return &organizationService{
svc: svc,
}
}
func (s *organizationService) GetAllTypes() ([]entity.OrganizationType, error) {
return s.svc.db.Organization().GetAllTypes()
}
func (s *organizationService) GetByType(organizationTypeKey string) ([]entity.Organization, error) {
return s.svc.db.Organization().GetByType(organizationTypeKey)
}
func (s *organizationService) GetByName(name string, searchType string) ([]entity.Organization, error) {
return s.svc.db.Organization().GetByName(name, searchType)
}
func (s *organizationService) GetByUUID(organizationUUID string) (entity.Organization, error) {
organization, err := s.svc.db.Organization().GetByUUID(organizationUUID)
if err != nil {
return organization, err
}
organization.Contacts, err = s.GetContactsByOrganizationID(organization.ID)
if err != nil {
return organization, err
}
organization.Addresses, err = s.GetAddressByOrganizationID(organization.ID)
if err != nil {
return organization, err
}
organization.ChildOrgs, err = s.svc.db.Organization().GetChildsByID(organization.ID)
if err != nil {
return organization, err
}
if organization.ParentID > 0 {
parent, err := s.svc.db.Organization().GetByID(organization.ParentID)
if err != nil {
return organization, err
}
organization.Parent = &parent
}
return organization, nil
}
func (s *organizationService) SetParentOrganization(organizationID int64, parentOrganizationID int64) error {
return s.svc.db.Organization().SetParentOrganization(organizationID, parentOrganizationID)
}
func (s *organizationService) GetContactsByOrganizationUUID(organizationUUID string) ([]entity.OrganizationContact, error) {
return s.svc.db.Organization().GetContactsByOrganizationUUID(organizationUUID)
}
func (s *organizationService) GetContactsByOrganizationID(organizationID int64) ([]entity.OrganizationContact, error) {
return s.svc.db.Organization().GetContactsByOrganizationID(organizationID)
}
func (s *organizationService) GetContactsByUUID(contactUUID string) (entity.OrganizationContact, error) {
return s.svc.db.Organization().GetContactsByUUID(contactUUID)
}
func (s *organizationService) GetAddressByOrganizationUUID(organizationUUID string) ([]entity.OrganizationAddress, error) {
return s.svc.db.Organization().GetAddressByOrganizationUUID(organizationUUID)
}
func (s *organizationService) GetAddressByOrganizationID(organizationID int64) ([]entity.OrganizationAddress, error) {
return s.svc.db.Organization().GetAddressByOrganizationID(organizationID)
}
func (s *organizationService) GetAddressByUUID(contactUUID string) (entity.OrganizationAddress, error) {
return s.svc.db.Organization().GetAddressByUUID(contactUUID)
}
func (s *organizationService) InactivateOrganizationAddress(address entity.OrganizationAddress) error {
return s.svc.db.Organization().InactivateOrganizationAddress(address)
}
func (s *organizationService) SetOrganizationAddress(address entity.OrganizationAddress) (entity.OrganizationAddress, error) {
return s.svc.db.Organization().SetOrganizationAddress(address)
}
func (s *organizationService) InactivateOrganizationContact(contact entity.OrganizationContact) error {
return s.svc.db.Organization().InactivateOrganizationContact(contact)
}
func (s *organizationService) SetOrganizationContact(contact entity.OrganizationContact) (entity.OrganizationContact, error) {
return s.svc.db.Organization().SetOrganizationContact(contact)
}
func (s *organizationService) AddOrganization(organization entity.Organization) (entity.Organization, error) {
return s.svc.db.Organization().AddOrganization(organization)
}
func (s *organizationService) GetTypeByKey(key string) (entity.OrganizationType, error) {
return s.svc.db.Organization().GetTypeByKey(key)
}

33
domain/service/profile.go Normal file
View File

@@ -0,0 +1,33 @@
package service
import (
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)
// userService is the domain service for user operations
type profileService struct {
svc *Service
}
// newUserService returns an instance of userService
func newProfileService(svc *Service) *profileService {
return &profileService{
svc: svc,
}
}
func (s *profileService) GetAll() ([]entity.Profile, error) {
return s.svc.db.Profile().GetAll()
}
func (s *profileService) GetByKey(key string) (entity.Profile, error) {
return s.svc.db.Profile().GetByKey(key)
}
func (s *profileService) GetVisibles(visible bool) ([]entity.Profile, error) {
return s.svc.db.Profile().GetVisibles(visible)
}
func (s *profileService) GetByOrganizationType(organizationTypeID int64) ([]entity.Profile, error) {
return s.svc.db.Profile().GetByOrganizationType(organizationTypeID)
}

View File

@@ -0,0 +1,46 @@
package service
import (
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)
// userService is the domain service for user operations
type providerService struct {
svc *Service
}
// newUserService returns an instance of userService
func newProviderService(svc *Service) *providerService {
return &providerService{
svc: svc,
}
}
// Save the ride for a expected user
func (s *providerService) Save(providers []entity.ProviderResponse, user entity.User) ([]entity.Provider, error) {
tx, err := s.svc.db.Begin()
if err != nil {
return nil, err
}
newProviders, err := tx.Provider().Save(providers, user)
if err != nil {
tx.Rollback()
return nil, err
}
tx.Commit()
return newProviders, nil
}
func (s *providerService) GetAll(user entity.User) ([]entity.Provider, error) {
return s.svc.db.Provider().GetAll(user)
}
func (s *providerService) Get(query string, lat float64, long float64, distance int64, planCode string, productID string, mukID string, internalID string, sort string, user entity.User) ([]entity.Provider, error) {
return s.svc.db.Provider().Get(query, lat, long, distance, planCode, productID, mukID, internalID, sort, user)
}
func (s *providerService) GetByMukID(mukID string, user entity.User) (entity.Provider, error) {
return s.svc.db.Provider().GetByMukID(mukID, user)
}

103
domain/service/ride.go Normal file
View File

@@ -0,0 +1,103 @@
package service
import (
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)
// userService is the domain service for user operations
type rideService struct {
svc *Service
}
// newUserService returns an instance of userService
func newRideService(svc *Service) *rideService {
return &rideService{
svc: svc,
}
}
// Save the ride for a expected user
func (s *rideService) Save(ride entity.Ride) (entity.Ride, error) {
ride, err := s.svc.db.Rides().Save(ride)
if err != nil {
return ride, err
}
user, err := s.svc.db.Users().GetByID(ride.CreatedUser.ID)
if err != nil {
return ride, err
}
return s.GetByUUID(ride.UUID, user)
}
func (s *rideService) Update(hook entity.WebhookResponse) (entity.Ride, error) {
user, err := s.svc.db.Users().GetByID(hook.Ride.CreatedUser.ID)
if err != nil {
return entity.Ride{}, err
}
ride, err := s.svc.db.Rides().Update(hook, user)
if err != nil {
return ride, err
}
return s.GetByUUID(ride.UUID, user)
}
// GetAll return all rides
func (s *rideService) GetAll(user entity.User) ([]entity.Ride, error) {
return s.svc.db.Rides().GetAll(user)
}
func (s *rideService) GetByID(id int64, user entity.User) (entity.Ride, error) {
return s.svc.db.Rides().GetByID(id, user)
}
// GetByUUID return a specific ride
func (s *rideService) GetByUUID(uuid string, user entity.User) (entity.Ride, error) {
return s.svc.db.Rides().GetByUUID(uuid, user)
}
// GetByUUID return a specific ride
func (s *rideService) GetByUUIDAndUserUUID(UUID string, userUUID string) (entity.Ride, error) {
return s.svc.db.Rides().GetByUUIDAndUserUUID(UUID, userUUID)
}
// GetByUUID return a specific ride
func (s *rideService) GetByInternalID(internalID string) (entity.Ride, error) {
return s.svc.db.Rides().GetByInternalID(internalID)
}
// GetByUserID return a list of rides
func (s *rideService) GetByUserID(userID int64, user entity.User) ([]entity.Ride, error) {
return s.svc.db.Rides().GetByUserID(userID, user)
}
func (s *rideService) GetByUserUUID(userUUID string, user entity.User) ([]entity.Ride, error) {
return s.svc.db.Rides().GetByUserUUID(userUUID, user)
}
func (s *rideService) UpdateStatus(rideUUID string, status string) error {
return s.svc.db.Rides().UpdateStatus(rideUUID, status)
}
func (s *rideService) GetLastRideByPhoneNumber(phoneNumber string) (entity.Ride, error) {
return s.svc.db.Rides().GetLastRideByPhoneNumber(phoneNumber)
}
func (s *rideService) GetLastRideByDriversNumber(phoneNumber string) (entity.Ride, error) {
return s.svc.db.Rides().GetLastRideByDriversNumber(phoneNumber)
}
func (s *rideService) GetByInternalPassengerID(internalPassengerID string) (entity.Ride, error) {
return s.svc.db.Rides().GetByInternalPassengerID(internalPassengerID)
}
func (s *rideService) GetByVisitUUID(visitUUID string, user entity.User) ([]entity.Ride, error) {
return s.svc.db.Rides().GetByVisitUUID(visitUUID, user)
}
func (s *rideService) GetByVisitUUIDAndTripType(visitUUID string, tripTypeKey string, user entity.User) (entity.Ride, error) {
return s.svc.db.Rides().GetByVisitUUIDAndTripType(visitUUID, tripTypeKey, user)
}

45
domain/service/service.go Normal file
View File

@@ -0,0 +1,45 @@
package service
import (
"sync"
"bitbucket.org/nemt/nemt-portal-api/domain/contract"
"bitbucket.org/nemt/nemt-portal-api/infra/config"
"bitbucket.org/nemt/nemt-portal-api/infra/logger"
)
var (
instance *Service
once sync.Once
)
// Service holds the domain service repositories
type Service struct {
db contract.DataManager
cache contract.CacheManager
tnc contract.TNCManager
Users *userService
Rides *rideService
Visits *visitService
Provider *providerService
Notification *notificationService
Profile *profileService
Organization *organizationService
}
// New returns a new domain Service instance
func New(db contract.DataManager, cache contract.CacheManager, cfg *config.Config, log *logger.Logger) (*Service, error) {
once.Do(func() {
instance = &Service{db: db, cache: cache}
instance.Users = newUserService(instance)
instance.Rides = newRideService(instance)
instance.Visits = newVisitService(instance)
instance.Provider = newProviderService(instance)
instance.Notification = newNotificationService(instance)
instance.Profile = newProfileService(instance)
instance.Organization = newOrganizationService(instance)
})
return instance, nil
}

28
domain/service/tnc.go Normal file
View File

@@ -0,0 +1,28 @@
package service
// tncService is the domain service for transportation network operations
type tncService struct {
svc *Service
}
// newTncService returns an instance of tncService
func newTncService(svc *Service) *tncService {
return &tncService{
svc: svc,
}
}
//GetETA will return the list of ETA's for the current location
func (s *tncService) GetETA(lag float64, log float64, params map[string]interface{}) (interface{}, error) {
return s.svc.tnc.GetETA(lag, log, params)
}
//GetDrivers return the drivers for the current location
func (s *tncService) GetDrivers(lag float64, log float64) (interface{}, error) {
return s.svc.tnc.GetDrivers(lag, log)
}
//GetTypes will return the available types of ride for the current location
func (s *tncService) GetTypes(lag float64, log float64, params map[string]interface{}) (interface{}, error) {
return s.svc.tnc.GetTypes(lag, log, params)
}

92
domain/service/user.go Normal file
View File

@@ -0,0 +1,92 @@
package service
import (
"fmt"
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)
// userService is the domain service for user operations
type userService struct {
svc *Service
}
// newUserService returns an instance of userService
func newUserService(svc *Service) *userService {
return &userService{
svc: svc,
}
}
// GetAll returns a list of users
func (s *userService) GetAll() (list []entity.User, err error) {
return s.svc.db.Users().GetAll()
}
// GetByID returns a specific user by its ID
func (s *userService) GetByID(userID int64) (entity.User, error) {
return s.svc.db.Users().GetByID(userID)
}
// GetByID returns a specific user by its ID
func (s *userService) GetByUUID(uuid string, profile string) (entity.User, error) {
return s.svc.db.Users().GetByUUID(uuid, profile)
}
// Login returns a specific user by email and pass
func (s *userService) Login(email string, pass string) (entity.User, error) {
return s.svc.db.Users().Login(email, pass)
}
// Login returns a specific user by email and pass
func (s *userService) FullLogin(loginType string, key string, pass string, profile string) (entity.User, error) {
return s.svc.db.Users().FullLogin(loginType, key, pass, profile)
}
// Login returns a specific user by email and pass
func (s *userService) Create(user entity.User) (entity.User, error) {
return s.svc.db.Users().Create(user)
}
func (s *userService) CreateBulk(users []entity.User) ([]entity.User, error) {
tx, err := s.svc.db.Begin()
if err != nil {
return nil, err
}
for i, _ := range users {
users[i], err = tx.Users().Create(users[i])
if err != nil {
fmt.Println(fmt.Sprintf("Email %s got error: %s", users[i].Email, err.Error()))
tx.Rollback()
return nil, err
}
fmt.Println(fmt.Sprintf("Email %s created", users[i].Email))
}
tx.Commit()
return users, nil
}
// GetUsersByProfile returns a list of users by profile
func (s *userService) GetUsersByProfile(profile string) ([]entity.User, error) {
return s.svc.db.Users().GetUsersByProfile(profile)
}
func (s *userService) RemoveAddress(addressUUID string) error {
return s.svc.db.Users().RemoveAddress(addressUUID)
}
// SaveAddress returns a list of users by profile
func (s *userService) SaveAddress(address entity.Address) (entity.Address, error) {
return s.svc.db.Users().SaveAddress(address)
}
// GetAddressByUUID returns a list of users by profile
func (s *userService) GetAddressByUUID(addressUUID string) (entity.Address, error) {
return s.svc.db.Users().GetAddressByUUID(addressUUID)
}
func (s *userService) GetContactType() (retVal []entity.ContactType, err error) {
return s.svc.db.Users().GetContactType()
}

81
domain/service/visit.go Normal file
View File

@@ -0,0 +1,81 @@
package service
import (
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
"bitbucket.org/nemt/nemt-portal-api/infra/errors"
)
// userService is the domain service for user operations
type visitService struct {
svc *Service
}
// newUserService returns an instance of userService
func newVisitService(svc *Service) *visitService {
return &visitService{
svc: svc,
}
}
// Save the ride for a expected user
func (s *visitService) Create(visit entity.Visit) (entity.Visit, error) {
return s.svc.db.Visits().Create(visit)
}
// Save the ride for a expected user
func (s *visitService) GetAll(user entity.User) ([]entity.Visit, error) {
visit, err := s.svc.db.Visits().GetAll(user)
if err != nil {
return nil, err
}
rides, err := s.svc.db.Rides().GetAll(user)
if err != nil {
return nil, err
}
ridesByVisit := make(map[int64][]entity.Ride)
for _, r := range rides {
rides := ridesByVisit[r.Visit.ID]
rides = append(rides, r)
ridesByVisit[r.Visit.ID] = rides
}
for i, v := range visit {
visit[i].Rides = ridesByVisit[v.ID]
}
return visit, nil
}
// Save the ride for a expected user
func (s *visitService) GetByUUID(visitUUID string, user entity.User) (entity.Visit, error) {
visit, err := s.svc.db.Visits().GetByUUID(visitUUID, user)
if err != nil {
return entity.Visit{}, errors.Wrap(err)
}
rides, err := s.svc.db.Rides().GetByVisitUUID(visitUUID, user)
if err != nil {
return entity.Visit{}, errors.Wrap(err)
}
visit.Rides = rides
return visit, nil
}
// Save the ride for a expected user
func (s *visitService) GetByID(visitID int64, user entity.User) (entity.Visit, error) {
visit, err := s.svc.db.Visits().GetByID(visitID, user)
if err != nil {
return entity.Visit{}, errors.Wrap(err)
}
rides, err := s.svc.db.Rides().GetByVisitUUID(visit.UUID, user)
if err != nil {
return entity.Visit{}, errors.Wrap(err)
}
visit.Rides = rides
return visit, nil
}