Skip to content

Commit

Permalink
[extapi] added books
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Mar 20, 2024
1 parent f50ac64 commit 98edfca
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cmd/servus-extapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func Version(c *gin.Context) {
rs := map[string]any{
"version": "v0.0.15",
"version": "v0.0.16",
}
c.AbortWithStatusJSON(200, rs)
}
Expand Down Expand Up @@ -46,6 +46,7 @@ func main() {
adapter.RaRoute(g, "youtubedl", model.YoutubeDL{})
adapter.RaRoute(g, "tables", model.Table{})
adapter.RaRoute(g, "tableparticipants", model.TableParticipant{})
adapter.RaRoute(g, "books", model.Book{})

g.POST("/bgstatsupload", adapter.G(CreateBGStats))

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/bgstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"github.com/DictumMortuum/servus/pkg/models"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -80,7 +81,7 @@ func (obj BGStat) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&BGStat{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/bgstatsgames.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"github.com/DictumMortuum/servus/pkg/models"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -81,7 +82,7 @@ func (obj BGStatsGame) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&BGStatsGame{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/bgstatslocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"github.com/DictumMortuum/servus/pkg/models"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -77,7 +78,7 @@ func (obj BGStatsLocation) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&BGStatsLocation{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/bgstatsplayers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"github.com/DictumMortuum/servus/pkg/models"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func (obj BGStatsPlayer) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&BGStatsPlayer{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/bgstatsplays.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"github.com/DictumMortuum/servus/pkg/models"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (obj BGStatsPlay) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&BGStatsPlay{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/boardgameprices.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (obj BoardgamePrice) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&BoardgamePrice{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/boardgames.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (obj Boardgame) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&Boardgame{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
85 changes: 85 additions & 0 deletions pkg/model/books.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package model

import (
"encoding/json"

"gorm.io/gorm"
)

type Book struct {
Id int64 `gorm:"primaryKey" json:"id,omitempty"`
Category string `json:"category,omitempty"`
Name string `json:"name,omitempty"`
ISBN string `json:"isbn,omitempty"`
Publisher string `json:"publisher,omitempty"`
Translation string `json:"translation,omitempty"`
Writer string `json:"writer,omitempty"`
}

func (Book) TableName() string {
return "tbooks"
}

func (Book) DefaultFilter(db *gorm.DB) *gorm.DB {
return db
}

func (c Book) List(db *gorm.DB, scopes ...func(*gorm.DB) *gorm.DB) (any, error) {
var data []Book
rs := db.Scopes(scopes...).Scopes(c.DefaultFilter).Find(&data)
return data, rs.Error
}

func (Book) Get(db *gorm.DB, id int64) (any, error) {
var data Book
rs := db.First(&data, id)
return data, rs.Error
}

func (obj Book) Update(db *gorm.DB, id int64, body []byte) (any, error) {
model := Book{
Id: id,
}

var payload map[string]any
err := json.Unmarshal(body, &payload)
if err != nil {
return nil, err
}

rs := db.Model(&model).Save(payload)
if rs.Error != nil {
return nil, err
}

return obj.Get(db, id)
}

func (Book) Create(db *gorm.DB, body []byte) (any, error) {
var payload Book
err := json.Unmarshal(body, &payload)
if err != nil {
return nil, err
}

rs := db.Create(&payload)
if rs.Error != nil {
return nil, err
}

return payload, nil
}

func (obj Book) Delete(db *gorm.DB, id int64) (any, error) {
data, err := obj.Get(db, id)
if err != nil {
return nil, err
}

rs := db.Delete(&IgnoredName{}, id)
if rs.Error != nil {
return nil, rs.Error
}

return data, nil
}
3 changes: 2 additions & 1 deletion pkg/model/ignorednames.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"gorm.io/gorm"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func (obj IgnoredName) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&IgnoredName{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/ignoredprices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"gorm.io/gorm"
)

Expand Down Expand Up @@ -72,7 +73,7 @@ func (obj IgnoredPrice) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&IgnoredPrice{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (obj Location) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&Location{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/model/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"encoding/json"

"gorm.io/gorm"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func (obj Player) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&Player{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/plays.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (obj Play) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&Play{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (obj Stat) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&Stat{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (obj Store) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&Store{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/tableparticipant.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (obj TableParticipant) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&TableParticipant{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/youtubedl.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (obj YoutubeDL) Delete(db *gorm.DB, id int64) (any, error) {
}

rs := db.Delete(&YoutubeDL{}, id)
if err != nil {
if rs.Error != nil {
return nil, rs.Error
}

Expand Down

0 comments on commit 98edfca

Please sign in to comment.