第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

gorm :單獨的數(shù)據(jù)庫存儲模型和查詢發(fā)送模型

gorm :單獨的數(shù)據(jù)庫存儲模型和查詢發(fā)送模型

Go
手掌心 2023-03-15 14:42:51
發(fā)送 gorm 模型的有效方法是什么,該模型與用于將數(shù)據(jù)存儲在數(shù)據(jù)庫中的 gorm 模型不同?該Post模型(結(jié)構(gòu))用于將數(shù)據(jù)存儲在數(shù)據(jù)庫中。type Post struct {    gorm.Model    Title string     Body  string }但我想使用這個PostReadgorm 模型,而不是發(fā)送回用戶。因為以后可能會有額外的變量添加到這個數(shù)據(jù)庫中,這些變量不會存儲在這個數(shù)據(jù)庫中。(或者額外的變量可能來自其他服務(wù)或數(shù)據(jù)庫)type PostRead struct {    ID    string `json:"id" gorm:"primary_key"`    Title string `json:"title"`    Body  string `json:"body"`    CreatedAt time.Time `json:"createAt"`    UpdatedAt time.Time `json:"updatedAt"`    ...    <other variables to be added later>}這是我當(dāng)前的控制器代碼,用于發(fā)回數(shù)據(jù):func PostGetAll(c *gin.Context) {    posts, err := services.PostGetAllService()    if err != nil {        c.JSON(http.StatusInternalServerError, gin.H{"error": err})        return    }    c.JSON(http.StatusOK, gin.H{"posts": posts})}這是我的服務(wù)代碼,用于從數(shù)據(jù)庫中獲取數(shù)據(jù):func PostGetAllService() ([]models.Post, error) {    var posts []models.Post    result := initializers.DB.Find(&posts)    if result.Error != nil {        return nil, errors.New(result.Error.Error())    }    return posts, nil}注意:我知道我可以PostRead在控制器函數(shù)中創(chuàng)建一個 SliceArray PostGetAll(),然后用于append()將來自帖子 () 的數(shù)據(jù)添加Post到 SliceArray 中。但對我來說,這似乎是解決這個問題的豐富方法,因此我想知道是否存在更好的解決方案?更高效的解決方案
查看完整描述

1 回答

?
繁花不似錦

TA貢獻1851條經(jīng)驗 獲得超4個贊

不完全確定您的問題,但無論如何讓我嘗試提供幫助。


通常我會執(zhí)行以下操作,但我相信有更好的方法可以做到。


對于模型,我將在如下所示的 pkg 中準(zhǔn)備它們。


// For reading data from form.

type CreatePostForm struct {

    Title string `json:"title"`

    Body  string `json:"body"`

}


// For interactions with database.

type Post struct {

    gorm.Model

    Title string 

    Body  string

}


type Posts []Post


// For data transfer object (sending over to UI etc).

type PostDto struct {

    Title       string 

    Body        string

    CreatedDate time.Time

    CreatedBy   string

}


type PostsDto []PostDto


// Convert form data into database model for database operations.

func (p PostCreateForm) ToCreateModel() (*Post, error) {

    // Process logic here which is basically creating a new Post model and returning it..

}


// Convert database model into DTO to reply to request etc.

func (p Post) ToDto() (*PostDto, error) {

    // Process logic here which is basically creating a new PostDto model and returning it..

}


// Convert many Posts to many DTOs.

func (ps Posts) ToDto() PostsDto {

    dtos := make(PostsDto, len(ps))

    for key, post := range ps {

        dtos[key] = post.ToDto()

    }


    return dtos

}

所以基本上從上面你從數(shù)據(jù)庫中獲取數(shù)據(jù)后,你基本上可以通過使用類似的方法將 Posts 類型轉(zhuǎn)換為 PostsDto 類型Posts.ToDto()。


數(shù)據(jù)庫適配器將在另一個 pkg 中,它基本上完成讀取/寫入數(shù)據(jù)庫的工作,我不會分享,因為你的問題更多是發(fā)送另一組數(shù)據(jù)用于 CRUD 操作。


同樣,我希望這對您有所幫助,但我仍然認為可能有更好的方法來做到這一點。


查看完整回答
反對 回復(fù) 2023-03-15
  • 1 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號