Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server/ui: patch artist/media/author tags #73

Merged
merged 9 commits into from
Aug 24, 2024
90 changes: 73 additions & 17 deletions server/artists.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,90 @@ func GetArtist(ctx context.Context, input *GetArtistInput) (*ArtistOutput, error
return artist_output, nil
}

type ArtistInfo struct {
Name string `json:"name"`
AdditionalNames []string `json:"additional_names"`
}

type CreateArtistInput struct {
Body struct {
Name string `json:"name"`
AdditionalNames []string `json:"additional_names"`
}
Body ArtistInfo
}

func createArtist(tx *gorm.DB, name string, additional_names []string, artist *Artist) error {
return tx.Transaction(
func(tx *gorm.DB) error {
additional_names_db := createAdditionalNames(additional_names)
artist.Name = name
artist.AdditionalNames = additional_names_db
err := tx.Create(artist).Error
return DBErrToHumaErr(err)
})
func createArtist(db *gorm.DB, artist *Artist, info *ArtistInfo) error {
return db.Transaction(func(tx *gorm.DB) error {
if err := info.to_Artist(artist); err != nil {
return err
}
err := tx.Create(artist).Error
return DBErrToHumaErr(err)
})
}

func CreateArtist(ctx context.Context, input *CreateArtistInput) (*ArtistOutput, error) {
artist_output := &ArtistOutput{}
db := GetDB(ctx)
output := ArtistOutput{}

err := db.Transaction(func(tx *gorm.DB) error {
artist := Artist{}
if err := createArtist(tx, &artist, &input.Body); err != nil {
return err
}
output.Body.Artist = artist
return nil
})

return &output, err
}

type UpdateArtistInput struct {
Id uint `path:"id"`
Body ArtistInfo
}

func updateArtist(tx *gorm.DB, artist *Artist) error {
err := tx.Model(&artist).Select("*").Updates(&artist).Error
if err != nil {
return err
}
prev_context := tx.Statement.Context
tx = WithAssociationsUpdate(tx)
defer tx.WithContext(prev_context)
err = tx.Model(&artist).Association("AdditionalNames").Replace(&artist.AdditionalNames)
if err != nil {
return err
}
return nil
}

func UpdateArtist(ctx context.Context, input *UpdateArtistInput) (*ArtistOutput, error) {
db := GetDB(ctx)
err := createArtist(db, input.Body.Name, input.Body.AdditionalNames, &artist_output.Body.Artist)
artist := Artist{}
err := db.First(&artist, input.Id).Error
if err != nil {
return nil, DBErrToHumaErr(err)
return nil, err
}
err = input.Body.to_Artist(&artist)
if err != nil {
return nil, err
}

return artist_output, nil
err = db.Transaction(func(tx *gorm.DB) error {
return updateArtist(tx, &artist)
})
if err != nil {
return nil, err
}

out := &ArtistOutput{}
out.Body.Artist = artist

return out, nil
}

func (info ArtistInfo) to_Artist(artist *Artist) error {
artist.Name = info.Name
artist.AdditionalNames = createAdditionalNames(info.AdditionalNames)
return nil
}

type DeleteArtistResponse struct {
Expand Down
72 changes: 61 additions & 11 deletions server/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,74 @@ func GetAuthor(ctx context.Context, input *GetAuthorInput) (*AuthorOutput, error
return author_output, nil
}

type AuthorInfo struct {
Name string `json:"name"`
}

type CreateAuthorInput struct {
Body struct {
Name string `json:"name"`
}
Body AuthorInfo
}

func CreateAuthor(ctx context.Context, input *CreateAuthorInput) (*AuthorOutput, error) {
db := GetDB(ctx)
author_output := &AuthorOutput{}
output := AuthorOutput{}

err := db.Transaction(func(tx *gorm.DB) error {
author := TimingAuthor{}
err := input.Body.to_TimingAuthor(&author)
if err != nil {
return err
}
output.Body.Author = author

err = tx.Create(&output.Body.Author).Error
return err
})

return &output, err
}

type UpdateAuthorInput struct {
Id uint `path:"id"`
Body AuthorInfo
}

func updateAuthor(tx *gorm.DB, author *TimingAuthor) error {
err := tx.Model(&author).Select("*").Updates(&author).Error
if err != nil {
return err
}
return nil
}

err := db.Transaction(
func(tx *gorm.DB) error {
author_output.Body.Author = TimingAuthor{Name: input.Body.Name}
err := tx.Create(&author_output.Body.Author).Error
return DBErrToHumaErr(err)
})
func UpdateAuthor(ctx context.Context, input *UpdateAuthorInput) (*AuthorOutput, error) {
db := GetDB(ctx)
author := TimingAuthor{}
err := db.First(&author, input.Id).Error
if err != nil {
return nil, err
}
err = input.Body.to_TimingAuthor(&author)
if err != nil {
return nil, err
}

err = db.Transaction(func(tx *gorm.DB) error {
return updateAuthor(tx, &author)
})
if err != nil {
return nil, err
}

out := &AuthorOutput{}
out.Body.Author = author

return out, nil
}

return author_output, err
func (info AuthorInfo) to_TimingAuthor(author *TimingAuthor) error {
author.Name = info.Name
return nil
}

type DeleteAuthorResponse struct {
Expand Down
3 changes: 3 additions & 0 deletions server/karaberus.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ func addRoutes(api huma.API) {
huma.Get(api, "/api/tags/author/search", FindAuthor, setSecurity(kara_ro_security))
huma.Get(api, "/api/tags/author/{id}", GetAuthor, setSecurity(kara_ro_security))
huma.Delete(api, "/api/tags/author/{id}", DeleteAuthor, setSecurity(kara_security))
huma.Patch(api, "/api/tags/author/{id}", UpdateAuthor, setSecurity(kara_security))
huma.Post(api, "/api/tags/author", CreateAuthor, setSecurity(kara_security))

huma.Get(api, "/api/tags/artist", GetAllArtists, setSecurity(kara_ro_security))
huma.Get(api, "/api/tags/artist/search", FindArtist, setSecurity(kara_ro_security))
huma.Get(api, "/api/tags/artist/{id}", GetArtist, setSecurity(kara_ro_security))
huma.Delete(api, "/api/tags/artist/{id}", DeleteArtist, setSecurity(kara_security))
huma.Patch(api, "/api/tags/artist/{id}", UpdateArtist, setSecurity(kara_security))
huma.Post(api, "/api/tags/artist", CreateArtist, setSecurity(kara_security))

huma.Get(api, "/api/tags/media", GetAllMedias, setSecurity(kara_ro_security))
huma.Get(api, "/api/tags/media/types", GetAllMediaTypes, setSecurity(kara_ro_security))
huma.Get(api, "/api/tags/media/search", FindMedia, setSecurity(kara_ro_security))
huma.Get(api, "/api/tags/media/{id}", GetMedia, setSecurity(kara_ro_security))
huma.Delete(api, "/api/tags/media/{id}", DeleteMedia, setSecurity(kara_security))
huma.Patch(api, "/api/tags/media/{id}", UpdateMedia, setSecurity(kara_security))
huma.Post(api, "/api/tags/media", CreateMedia, setSecurity(kara_security))

huma.Post(api, "/api/mugen", ImportMugenKara, setSecurity(kara_security))
Expand Down
93 changes: 75 additions & 18 deletions server/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"gorm.io/gorm"
)

type MediaInfo struct {
Name string `json:"name" example:"Shinseiki Evangelion"`
MediaType string `json:"media_type" example:"ANIME"`
AdditionalNames []string `json:"additional_names" example:"[]"`
}

type CreateMediaInput struct {
Body struct {
Name string `json:"name" example:"Shinseiki Evangelion"`
MediaType string `json:"media_type" example:"ANIME"`
AdditionalNames []string `json:"additional_names" example:"[]"`
}
Body MediaInfo
}

type MediaOutput struct {
Expand Down Expand Up @@ -44,27 +46,82 @@ func getMediaByID(tx *gorm.DB, Id uint) (MediaDB, error) {
// return media, DBErrToHumaErr(err)
// }

func createMedia(tx *gorm.DB, name string, media_type MediaType, additional_names []string, media *MediaDB) error {
err := tx.Transaction(func(tx *gorm.DB) error {
media.Name = name
media.Type = media_type.ID
media.AdditionalNames = createAdditionalNames(additional_names)

err := tx.Create(&media).Error
func createMedia(db *gorm.DB, media *MediaDB, info *MediaInfo) error {
return db.Transaction(func(tx *gorm.DB) error {
if err := info.to_MediaDB(media); err != nil {
return err
}
err := tx.Create(media).Error
return DBErrToHumaErr(err)
})

return err
}

func CreateMedia(ctx context.Context, input *CreateMediaInput) (*MediaOutput, error) {
media_output := &MediaOutput{}
media_type := getMediaType(input.Body.MediaType)
db := GetDB(ctx)
output := MediaOutput{}

err := db.Transaction(func(tx *gorm.DB) error {
media := MediaDB{}
if err := createMedia(tx, &media, &input.Body); err != nil {
return err
}
output.Body.Media = media
return nil
})

return &output, err
}

type UpdateMediaInput struct {
Id uint `path:"id"`
Body MediaInfo
}

func updateMedia(tx *gorm.DB, media *MediaDB) error {
err := tx.Model(&media).Select("*").Updates(&media).Error
if err != nil {
return err
}
prev_context := tx.Statement.Context
tx = WithAssociationsUpdate(tx)
defer tx.WithContext(prev_context)
err = tx.Model(&media).Association("AdditionalNames").Replace(&media.AdditionalNames)
if err != nil {
return err
}
return nil
}

func UpdateMedia(ctx context.Context, input *UpdateMediaInput) (*MediaOutput, error) {
db := GetDB(ctx)
err := createMedia(db, input.Body.Name, media_type, input.Body.AdditionalNames, &media_output.Body.Media)
media := MediaDB{}
err := db.First(&media, input.Id).Error
if err != nil {
return nil, err
}
err = input.Body.to_MediaDB(&media)
if err != nil {
return nil, err
}

err = db.Transaction(func(tx *gorm.DB) error {
return updateMedia(tx, &media)
})
if err != nil {
return nil, err
}

out := &MediaOutput{}
out.Body.Media = media

return out, nil
}

return media_output, err
func (info MediaInfo) to_MediaDB(media *MediaDB) error {
media.Name = info.Name
media.Type = getMediaType(info.MediaType).ID
media.AdditionalNames = createAdditionalNames(info.AdditionalNames)
return nil
}

type DeleteMediaResponse struct {
Expand Down
Loading
Loading