63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package db
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type LivreurRating struct {
|
|
ID int `json:"id"`
|
|
OrderID int `json:"order_id"`
|
|
LivreurUsername string `json:"livreur_username"`
|
|
ClientUsername string `json:"client_username"`
|
|
Rating int `json:"rating"`
|
|
Comment string `json:"comment"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (d *Database) SubmitLivreurRating(orderID int, livreurUsername, clientUsername string, rating int, comment string) error {
|
|
return d.GDB.Exec(`
|
|
INSERT INTO livreur_ratings (order_id, livreur_username, client_username, rating, comment, created_at)
|
|
VALUES (?, ?, ?, ?, ?, NOW())
|
|
`, orderID, livreurUsername, clientUsername, rating, comment).Error
|
|
}
|
|
|
|
func (d *Database) GetOrderRating(orderID int) (*LivreurRating, error) {
|
|
var r LivreurRating
|
|
err := d.GDB.Raw(`SELECT * FROM livreur_ratings WHERE order_id = ? LIMIT 1`, orderID).Scan(&r).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.ID == 0 {
|
|
return nil, nil
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
func (d *Database) GetLivreurRatings(livreurUsername string) ([]LivreurRating, float64, error) {
|
|
var ratings []LivreurRating
|
|
if err := d.GDB.Raw(`
|
|
SELECT * FROM livreur_ratings WHERE livreur_username = ? ORDER BY created_at DESC LIMIT 200
|
|
`, livreurUsername).Scan(&ratings).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var avg float64
|
|
if len(ratings) > 0 {
|
|
d.GDB.Raw(`SELECT COALESCE(AVG(rating), 0) FROM livreur_ratings WHERE livreur_username = ?`, livreurUsername).Scan(&avg)
|
|
}
|
|
return ratings, avg, nil
|
|
}
|
|
|
|
// GetOrderForRating retourne l'username client et le livreur d'une commande approuvée
|
|
func (d *Database) GetOrderForRating(orderID int) (clientUsername, livreurUsername string, err error) {
|
|
var row struct {
|
|
Username string `gorm:"column:username"`
|
|
LivreurAssign string `gorm:"column:livreur_assign"`
|
|
}
|
|
err = d.GDB.Raw(`
|
|
SELECT username, COALESCE(livreur_assign, '') as livreur_assign
|
|
FROM commandes WHERE id = ? AND status = 'approved' LIMIT 1
|
|
`, orderID).Scan(&row).Error
|
|
return row.Username, row.LivreurAssign, err
|
|
}
|