db

package
v0.0.0-...-5b3ac6b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Mongoclient *mongo.Client
	Ctx         context.Context

	CustomerCollection *mongo.Collection
	DriverCollection   *mongo.Collection
	OrderCollection    *mongo.Collection
	MerchantCollection *mongo.Collection
	MenuCollection     *mongo.Collection
	ReviewCollection   *mongo.Collection
	CategoryCollection *mongo.Collection
)

TODO: support db validation and error handling which almost return description what error happened

View Source
var CommonProtoctedFields []string = []string{"password", "device", "metadata.token_version", "metadata.provider"}
View Source
var Roles []string = []string{
	"customer",
	"driver",
	"merchant",
}

Functions

func CalculateRate

func CalculateRate(scores []int) int

Calculate all score and return five 5-star rating score i.e 2.9 ,3.7 etc.

func ChangeMenuField

func ChangeMenuField(filter bson.D, update bson.D) (*mongo.UpdateResult, error)

func CloseDB

func CloseDB() error

func ConnectDB

func ConnectDB()

func CreateGeoIndex

func CreateGeoIndex(name string, Collection *mongo.Collection)

func CreateIndex

func CreateIndex(name string, Collection *mongo.Collection)

func CustomerLoginCheck

func CustomerLoginCheck(email string, password string, device Device) (*TokenResponse, *ErrorResponse)

func DeleteReview

func DeleteReview(id primitive.ObjectID) (*mongo.DeleteResult, error)

delte review

func DriverLoginCheck

func DriverLoginCheck(email string, password string, device Device) (*TokenResponse, *ErrorResponse)

func GetDrivers

func GetDrivers(query bson.M, projection []string) ([]map[string]interface{}, error)

func GoogleLogin

func GoogleLogin(data GoogleUserInfoType) (*TokenResponse, *ErrorResponse)

func IsDup

func IsDup(err error) bool

func MerchantLoginCheck

func MerchantLoginCheck(email string, password string, device Device) (*TokenResponse, *ErrorResponse)

func ProtectFields

func ProtectFields(fields []string) primitive.D

func ReviewMenu

func ReviewMenu(id primitive.ObjectID)

func ReviewUpdate

func ReviewUpdate(filter bson.M, update bson.D) (*mongo.UpdateResult, error)

Update review

func TokenGenerate

func TokenGenerate(email string, id primitive.ObjectID, tokenVersion int) (*TokenResponse, *ErrorResponse)

func UpdateCustomer

func UpdateCustomer(query bson.M, change bson.D) (*mongo.UpdateResult, error)

func UpdateDriver

func UpdateDriver(query bson.M, change bson.D) (*mongo.UpdateResult, error)

func UpdateMerchant

func UpdateMerchant(query bson.M, change bson.D) (*mongo.UpdateResult, error)

func UpdateOrder

func UpdateOrder(query bson.M, change bson.D) (*mongo.UpdateResult, error)

Types

type AccountMetadata

type AccountMetadata struct {
	TokenVersion    int       `bson:"token_version" json:"-"`
	CreatedAt       time.Time `json:"created_at" bson:"created_at"`
	UpdatedAt       time.Time `json:"-" bson:"updated_at"`
	LasLogin        time.Time `bson:"last_login" json:"-"`
	WebhookEndpoint string    `json:"webhook_endpoint" bson:"webhook_endpoint"`
	Provider        string    `bson:"provider" json:"-"` // google , email , facebook
}

type ActiveDays

type ActiveDays struct {
	TimeOperatorStart float32 `bson:"time_operation_start" json:"time_operation_start"` // 7.10 => 07:19 UTC
	TimeOperatorEnd   float32 `bson:"time_operation_end" json:"time_operation_end"`
}

type Attributes

type Attributes struct {
	Volume float64 `bson:"volume" json:"volume"`
	Height float64 `bson:"height" json:"height"`
	Width  float64 `bson:"width" json:"width"`
	Length float64 `bson:"length" json:"length"`
}

TODO: support attributes for basic types and custom type for menues. all this values are represented as cm or centimeter as unit type

type Badge

type Badge struct {
	Title       string `bson:"title" json:"title"`
	Description string `bson:"description" json:"description"`
}

type Category

type Category struct {
	Id          primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	Title       string             `json:"title" bson:"title"`
	Description string             `json:"description" bson:"description"`
	Icon        string             `json:"icon" bson:"icon"`
}

func GetCategories

func GetCategories(query bson.D) ([]*Category, error)

type Coupon

type Coupon struct {
	Token             string    `json:"token" bson:"token"`
	Rate              uint      `json:"rate" bson:"rate"`                                 // percentage of the discount
	TimeOperatorStart time.Time `bson:"time_operation_start" json:"time_operation_start"` // Datetime Started
	TimeOperatorEnd   time.Time `bson:"time_operation_end" json:"time_operation_end"`     // Datetime End coupon expires
}

type Customer

type Customer struct {
	Id         primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	Email      string             `bson:"email,omitempty" json:"email"`
	FamilyName string             `bson:"family_name,omitempty" json:"family_name,omitempty"`
	GivenName  string             `bson:"given_name" json:"given_name"`
	Address    string             `bson:"address" json:"address"`
	Metadata   AccountMetadata    `bson:"metadata" json:"metadata"`
	Profile    string             `bson:"profile" json:"profile"`
	Password   string             `bson:"password" json:"-"`
	Device     Device             `bson:"device" json:"-"`
	Phone      string             `bson:"phone" json:"phone"`
	Preference []string           `bson:"preference" json:"preference"`
}

func (*Customer) BeforeSave

func (c *Customer) BeforeSave() error

func (*Customer) ChangeEmail

func (c *Customer) ChangeEmail(OldEmail string, NewEmail string) (*mongo.UpdateResult, *ErrorResponse)

func (*Customer) ChangeMetadataLogin

func (c *Customer) ChangeMetadataLogin() error

func (*Customer) ChangePassword

func (c *Customer) ChangePassword(OldPassword string, NewPassword string) *ErrorResponse

func (*Customer) Delete

func (c *Customer) Delete() (*mongo.DeleteResult, error)

func (*Customer) GetAll

func (c *Customer) GetAll() ([]*Customer, error)

func (*Customer) GetById

func (c *Customer) GetById() error

func (*Customer) Save

func (c *Customer) Save() (*mongo.InsertOneResult, error)

func (*Customer) Update

func (c *Customer) Update() (*mongo.UpdateResult, error)

type Device

type Device struct {
	DeviceId string `bson:"device_id" json:"device_id"`
	Kind     string `bson:"kind" json:"kind"` // andriod ,ios
}

type Driver

type Driver struct {
	Id        primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	Password  string             `json:"-" bson:"password"`
	Email     string             `json:"email" bson:"email"`
	Phone     string             `json:"phone" bson:"phone"`
	GivenName string             `json:"given_name" bson:"given_name"`
	Age       primitive.DateTime `json:"age" bson:"age"`
	Address   string             `json:"address" bson:"address"`
	Metadata  AccountMetadata    `json:"metadata" bson:"metadata"`
	Profile   string             `json:"profile" bson:"profile"`
	Device    Device             `json:"-" bson:"device"`
	Rate      Rate               `json:"rate" bson:"rate"`
	Location  Location           `json:"location" bson:"location"`
	Satus     bool               `json:"status" bson:"status"` // true available, false unavailable
	Vehicle   Vehicle            `json:"vehicle" bson:"vehicle"`
}

func GetByDriversLocation

func GetByDriversLocation(location []float64, maxdist int64, mindist int64) ([]*Driver, error)

func (*Driver) BeforeSave

func (d *Driver) BeforeSave() error

func (*Driver) ChangeEmail

func (d *Driver) ChangeEmail(OldEmail string, NewEmail string) (*mongo.UpdateResult, *ErrorResponse)

func (*Driver) ChangeMetadataLogin

func (d *Driver) ChangeMetadataLogin() error

func (*Driver) ChangePassword

func (d *Driver) ChangePassword(OldPassword string, NewPassword string) *ErrorResponse

func (*Driver) ChangePhone

func (d *Driver) ChangePhone(OldPhone string, NewPhone string) (*mongo.UpdateResult, error)

func (*Driver) Delete

func (d *Driver) Delete() (*mongo.DeleteResult, error)

func (*Driver) GetById

func (d *Driver) GetById() error

func (*Driver) Save

func (d *Driver) Save() (*mongo.InsertOneResult, error)

func (*Driver) Update

func (d *Driver) Update() (*mongo.UpdateResult, error)

type ErrorResponse

type ErrorResponse struct {
	Status  int
	Message error
	Type    string
}

func AssignOrderToDriver

func AssignOrderToDriver(orderId primitive.ObjectID, driverId primitive.ObjectID) (*mongo.UpdateResult, *ErrorResponse)

func DBErrorHandler

func DBErrorHandler(err error) *ErrorResponse

func DropOrder

func DropOrder(orderId primitive.ObjectID, driverId string) (*mongo.UpdateResult, *ErrorResponse)

drop the order like driver decline order after he accept

func (*ErrorResponse) Error

func (e *ErrorResponse) Error(c *gin.Context)

type GoogleUserInfoType

type GoogleUserInfoType struct {
	Id            string `json:"id"`
	Email         string `json:"email"`
	VerifiedEmail bool   `json:"verified_email"`
	Name          string `json:"name"`        // full name
	GivenName     string `json:"given_name"`  // first name
	FamilyName    string `json:"family_name"` // last name
	Picture       string `json:"picture"`
	Locale        string `json:"locale"`
}

type Item

type Item struct {
	ItemExternalId string `json:"item_external_id" bson:"item_external_id"`
	Quantity       uint   `json:"quantity" bson:"quantity"`
	Price          uint   `json:"price" bson:"price"`
}

type Location

type Location struct {
	Type        string    `bson:"type" json:"type"`
	Coordinates []float64 `bson:"coordinates" json:"coordinates"`
}
type Menu struct {
	Id                  primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	Title               string             `json:"title" bson:"title" binding:"max=30,min=1"` // Burger
	Description         string             `json:"description" bson:"description"`            // Chicken fries contains
	Status              string             `json:"status" bson:"status"`                      // available , unavailable , banned
	Category            string             `json:"category" bson:"category"`                  // fast food , drink ,
	Images              []string           `json:"images" bson:"images"`                      // Images of the product urls
	Price               uint               `json:"price" bson:"price"`                        // the price of the product is represented as cents 99 = $0.99
	Attributes          Attributes         `json:"attributes" bson:"attributes"`
	Metadata            Metadata           `json:"metadata" bson:"metadata"`
	MerchantExternalId  string             `json:"merchant_external_id" bson:"merchant_external_id"`
	Reciepe             []string           `json:"reciepe" bson:"reciepe"`             // floor , meat , egg etc.
	Barcode             string             `json:"-" bson:"barcode"`                   // if this needed
	EstimateTime        int                `json:"estimate_time" bson:"estimate_time"` // estimate of preparation and cooking time in seconds
	ServiceAvailability []ActiveDays       ` json:"service_availablity" bson:"service_availablity"`
	Likes               uint               `json:"likes" bson:"likes"` //
}

func GetCategoryMenues

func GetCategoryMenues(category string) ([]*Menu, error)

func GetListMenus

func GetListMenus(oids []primitive.ObjectID) ([]*Menu, error)

func GetMenues

func GetMenues(query bson.D) ([]*Menu, error)
func (m *Menu) Create() (*mongo.InsertOneResult, error)
func (m *Menu) Delete() (*mongo.DeleteResult, error)
func (m Menu) GetAll() ([]*Menu, error)
func (m *Menu) GetById() error
func (m Menu) GetFromMerchant() ([]*Menu, error)
func (m Menu) SetImages() (*mongo.UpdateResult, error)
func (m *Menu) Update() (*mongo.UpdateResult, error)

type Merchant

type Merchant struct {
	Id            primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` // 63f642ac061b6f5f089b3a65
	BusinessName  string             `bson:"business_name" json:"business_name"`
	BusinessEmail string             `json:"business_email" bson:"business_email"`
	BusinessPhone string             `bson:"business_phone" json:"business_phone"`
	Location      Location           `bson:"location" json:"location"`
	Address       string             `bson:"address" json:"address"`
	Metadata      AccountMetadata    `bson:"metadata" json:"metadata"`
	Password      string             `json:"-" bson:"password"`
	Profile       string             `json:"profile" bson:"profile"`
	Device        Device             `json:"-" bson:"device"`
	Rate          Rate               `json:"rate" bson:"rate"`
	Badge         Badge              `json:"badge" bson:"badge"`
	Closed        bool               `json:"closed" bson:"closed"`
	ActiveDays    []ActiveDays       `json:"active_days" bson:"active_days"`
	Generes       []string           `bson:"generes" json:"generes"` //Fast food.Fast casual.Casual dining / Slow Casual.Premium casual.Family style.Fine dining.
	Likes         uint               `bson:"likes" json:"likes"`
	Popular       uint               `bson:"popular" json:"popular"`
}

func FilterMerchants

func FilterMerchants(query bson.D, option *options.FindOptions) ([]*Merchant, error)

func GetMerchantByLocation

func GetMerchantByLocation(location []float64, maxdist int, mindist int) ([]*Merchant, error)

func (*Merchant) BeforeSave

func (m *Merchant) BeforeSave() error

func (*Merchant) ChangeBusinessPhone

func (m *Merchant) ChangeBusinessPhone(OldPhone string, NewPhone string) (*mongo.UpdateResult, error)

func (*Merchant) ChangeMetadataLogin

func (m *Merchant) ChangeMetadataLogin() error

func (*Merchant) ChangePassword

func (m *Merchant) ChangePassword(OldPassword string, NewPassword string) *ErrorResponse

func (*Merchant) Delete

func (m *Merchant) Delete() (*mongo.DeleteResult, error)

func (*Merchant) GetAll

func (m *Merchant) GetAll() ([]*Merchant, error)

func (*Merchant) GetById

func (m *Merchant) GetById() error

func (*Merchant) Save

func (*Merchant) Update

func (m *Merchant) Update() (*mongo.UpdateResult, error)

future Archive

type Metadata

type Metadata struct {
	CreatedAt time.Time `json:"created_at" bson:"created_at"`
	UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}

type Order

type Order struct {
	Id                    primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	DisplayId             string             `bson:"display_id" json:"display_id"`
	OrderValue            uint               `json:"order_value" bson:"order_value" ` // Order value is represented as cents 199 = $19.9
	Type                  string             `json:"type" bson:"type"`                // pickup or delivery order
	Items                 []Item             `json:"items" bson:"items"`
	DropOffPhone          string             `json:"dropoff_phone" bson:"dropoff_phone"`
	DropOffExteranlId     string             `json:"dropoff_external_id" bson:"dropoff_external_id"`
	DropOffContactName    string             `json:"dropoff_contact_name" bson:"dropoff_contact_name"`
	DropOffTimeEstimated  int                `json:"dropoff_time_estimated" bson:"dropoff_time_estimated"`
	DropOffTime           time.Time          `json:"dropoff_time" bson:"dropoff_time"`
	DropOffAddress        string             `json:"dropoff_address" bson:"dropoff_address"`   // address 901 Market Street 6th Floor San Francisco, CA 94103
	DroOffLocation        Location           `json:"dropoff_location" bson:"dropoff_location"` // location cordinates. float([123.1312343,-37.2144343])
	DropOffInstruction    string             `json:"dropoff_instructions" bson:"dropoff_instructions"`
	Stage                 string             `json:"stage" bson:"stage"`                                     // placed,accepted,preparing,ready,pickuped,deleivered,cancelled.
	ActionIfUndeliverable string             `json:"action_if_undeliverable" bson:"action_if_undeliverable"` // return_to_merchant // destroy
	PickupAddress         string             `json:"pickup_address" bson:"pickup_address"`
	PickUpExternalId      string             `bson:"pickup_external_id" json:"pickup_external_id"`
	PickUpName            string             `bson:"pickup_name" json:"pickup_name"`
	PickUpPhone           string             `bson:"pickup_phone" json:"pickup_phone"`
	PickUpLocation        Location           `bson:"pickup_location" json:"pickup_location"`
	PickupTime            time.Time          `bson:"pickup_time" json:"pickup_time"`
	PickupEstimatedTime   int                `bson:"pickup_estimated_time" json:"pickup_estimated_time"` // seconds
	DriverPhone           string             `bson:"driver_phone" json:"driver_phone"`
	DriverAllowedVehicles []string           `json:"driver_allowed_vehicles" bson:"driver_allowed_vehicles" ` // car , motorcycle , walking
	DriverExternalId      string             `bson:"driver_external_id" json:"driver_external_id"`
	Metadata              Metadata           `bson:"metadata" json:"metadata"`
	CancelReason          string             `bson:"cancel_reason" json:"cancel_reason"`
}

func GetOrderBy

func GetOrderBy(query bson.D) (*Order, error)

func GetOrdersBy

func GetOrdersBy(filter bson.D) ([]*Order, error)

func (*Order) BeforeSave

func (o *Order) BeforeSave() *ErrorResponse

func (*Order) CalculatePrice

func (o *Order) CalculatePrice()

func (*Order) ExtractItems

func (o *Order) ExtractItems() *ErrorResponse

func (*Order) GetById

func (o *Order) GetById() error

func (*Order) GetByLocation

func (o *Order) GetByLocation(location []float64, maxdist int64, mindist int64) ([]*Order, error)

func (*Order) PickuPExtract

func (o *Order) PickuPExtract() *ErrorResponse

func (*Order) PlaceOrder

func (o *Order) PlaceOrder() (*mongo.InsertOneResult, *ErrorResponse)

places order

type Rate

type Rate struct {
	Rate         float32 `bson:"rate" json:"rate"`
	ScoreStats   []int   `bson:"stats" json:"-"` // [20,20,0,10,5]
	Participants uint    `bson:"participants" json:"participants"`
}

type Review

type Review struct {
	Id         primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` // 63f642ac061b6f5f089b3a65
	Type       string             `bson:"type" json:"type"`                  // REVIEW_MERCHANT , REVIEW_DRIVER , REVIEW_MENU
	OrderId    string             `bson:"order_id" json:"order_id"`
	From       string             `bson:"from" json:"from"`
	Message    string             `bson:"message" json:"message"`
	Rate       uint               `bson:"rate" json:"rate"`
	ExternalId string             `bson:"external_id" json:"external_id"`
	Options    []string           `bson:"options" json:"options"`
	Metadata   Metadata           `bson:"metadata" json:"metadata"`
}

func GetReviewById

func GetReviewById(id primitive.ObjectID) (*Review, error)

func GetReviews

func GetReviews(query bson.D) ([]*Review, error)

get all the reviews

func (*Review) Create

func (r *Review) Create() (*mongo.InsertOneResult, error)

type Setting

type Setting struct {
	ReceiveNotification bool `bson:"receive_notification,omitempty" json:"receive_notification"`
	ReceiveUpdates      bool `bson:"receive_update" json:"receive_update"`
}

type TokenResponse

type TokenResponse struct {
	RefreshToken string `json:"refresh_token"`
	AccessToken  string `json:"access_token"`
}

type Vehicle

type Vehicle struct {
	Model   string  `json:"model" bson:"model"`     // Tesla , bmw ,
	Type    string  `json:"type" bson:"type"`       // car , motorbike , bicycle , truck , etc.
	Payload float64 `json:"payload" bson:"payload"` // how much payload can vehicle carry in kg
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL