| 361 | } |
| 362 | |
| 363 | func (hole *Hole) Create(c *fiber.Ctx, content string, specialTag string, db ...*gorm.DB) error { |
| 364 | var tx *gorm.DB |
| 365 | if len(db) > 0 { |
| 366 | tx = db[0] |
| 367 | } else { |
| 368 | tx = DB |
| 369 | } |
| 370 | |
| 371 | hole.UserID, _ = GetUserID(c) |
| 372 | |
| 373 | return tx.Transaction(func(tx *gorm.DB) error { |
| 374 | // Create hole |
| 375 | hole.Reply = -1 |
| 376 | result := tx.Omit("Tags").Create(hole) // tags are created in AfterCreate hook |
| 377 | if result.Error != nil { |
| 378 | return result.Error |
| 379 | } |
| 380 | hole.Reply = 0 |
| 381 | |
| 382 | // Bind and Create floor |
| 383 | floor := Floor{ |
| 384 | HoleID: hole.ID, |
| 385 | Content: content, |
| 386 | UserID: hole.UserID, |
| 387 | SpecialTag: specialTag, |
| 388 | } |
| 389 | |
| 390 | // create floor |
| 391 | err := floor.Create(c, tx) |
| 392 | if err != nil { |
| 393 | return err |
| 394 | } |
| 395 | |
| 396 | // create Favorite |
| 397 | return UserCreateFavourite(tx, c, false, hole.UserID, []int{hole.ID}) |
| 398 | }) |
| 399 | } |
| 400 | |
| 401 | func (hole *Hole) AfterCreate(tx *gorm.DB) (err error) { |
| 402 | hole.HoleID = hole.ID |