mirror of
https://github.com/m5stack/StackChan.git
synced 2026-04-27 19:12:40 +00:00
server code 4/27
This commit is contained in:
@@ -7,60 +7,75 @@ package dance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model"
|
||||
"stackChan/internal/model/do"
|
||||
|
||||
"stackChan/api/dance/v1"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
|
||||
if req.Index < 0 {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Index cannot be negative")
|
||||
// 1. Get and validate MAC address (business required parameter)
|
||||
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
|
||||
if mac == "" {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "MAC address cannot be empty")
|
||||
}
|
||||
|
||||
device, err := dao.Device.Ctx(ctx).Where("mac=", req.Mac).One()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 2. Auto validate using struct v tag (DanceName required), manual secondary validation as fallback
|
||||
if req.DanceName == "" {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance name cannot be empty")
|
||||
}
|
||||
|
||||
if device.IsEmpty() {
|
||||
_, err = dao.Device.Ctx(ctx).Data(dao.Device.Columns().Mac, req.Mac).Insert()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 3. Validate dance data not empty (RawMessage need to check if empty/only null)
|
||||
if len(req.DanceData) == 0 || string(req.DanceData) == "null" {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance data cannot be empty or null")
|
||||
}
|
||||
// 4. Use transaction to ensure data consistency
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// 4.1 Query device, create if not exists
|
||||
device, err := dao.Device.Ctx(ctx).TX(tx).Where("mac=?", mac).One()
|
||||
if err != nil && !gerror.HasCode(err, gcode.CodeNotFound) {
|
||||
return gerror.NewCode(gcode.CodeInternalError, "Failed to query device: %v", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
dance, err := dao.DeviceDance.Ctx(ctx).Where("mac=?", req.Mac).Where("dance_index=?", req.Index).One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Create device if not exists
|
||||
if device.IsEmpty() {
|
||||
_, err = dao.Device.Ctx(ctx).TX(tx).Data(dao.Device.Columns().Mac, mac).Insert()
|
||||
if err != nil {
|
||||
return gerror.NewCode(gcode.CodeInternalError, "Failed to create device: %v", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
danceListJSON, err := json.Marshal(req.List)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 4.2 Check if dance data with same MAC+DanceName exists (avoid duplicates)
|
||||
exist, err := dao.DeviceDance.Ctx(ctx).TX(tx).
|
||||
Where("mac=?", mac).
|
||||
Where("dance_name=?", req.DanceName).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return gerror.NewCode(gcode.CodeInternalError, "Failed to check duplicate dance data: %v", err.Error())
|
||||
}
|
||||
if exist {
|
||||
return gerror.NewCode(gcode.CodeBusinessValidationFailed, "Dance data with MAC %s and name '%s' already exists", mac, req.DanceName)
|
||||
}
|
||||
|
||||
if dance.IsEmpty() {
|
||||
_, err = dao.DeviceDance.Ctx(ctx).Data(do.DeviceDance{
|
||||
Mac: req.Mac,
|
||||
DanceIndex: req.Index,
|
||||
DanceData: danceListJSON,
|
||||
// 4.3 Insert dance data (use RawMessage directly, no need for secondary serialization)
|
||||
_, err = dao.DeviceDance.Ctx(ctx).TX(tx).Data(do.DeviceDance{
|
||||
Mac: mac,
|
||||
DanceData: req.DanceData, // Use RawMessage directly, avoid duplicate marshal
|
||||
DanceName: req.DanceName,
|
||||
MusicUrl: req.MusicUrl, // Add background music URL field
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
_, err = dao.DeviceDance.Ctx(ctx).Where("mac=?", req.Mac).Where("dance_index=?", req.Index).Data(do.DeviceDance{
|
||||
DanceData: danceListJSON,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return gerror.NewCode(gcode.CodeInternalError, "Failed to insert dance data: %v", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := v1.CreateRes("Dance data saved successfully")
|
||||
return &response, nil
|
||||
|
||||
Reference in New Issue
Block a user