How to Update Multiple Records Using gorm? - go

So I want to update movie_genre data with different genre_id values, each movie has more than 1 genre_id automatically there are multiple records in movie_genre with the same movie_id and different genre_id,
when I start trying to update with gorm by selecting only 1 genre_id, the old genre has changed, but the newly changed genre is created again in a new row with all the same column values ​​except created_at, whereas if I try to update by selecting 2 genres, the data the changed only has the value of the first genre_id, the second genre_id is not read
this is a erd database
payload json from frontend
{
"title":"Autaaa sint nihil quis ",
"release_date":"2001-01-01",
"runtime":"22",
"mpaa_rating":"G",
"rating":"4",
"description":"Eius enim distinctio1",
"id":"82",
"genre_id":["3","8"]
}:
handler
func (app *Application) UpdateMovie(ctx *gin.Context) {
var payload web.MoviePayloadResponse
if err := ctx.BindJSON(&payload); err != nil {
log.Println(err)
return
}
id, _ := strconv.Atoi(payload.ID)
movie, err := app.models.Repository.GetMovieById(id)
if err != nil {
helper.NotFound(*ctx, err)
return
}
movie.ID, _ = strconv.Atoi(payload.ID)
movie.Title = payload.Title
movie.Description = payload.Description
movie.ReleaseDate, _ = time.Parse("2006-01-02", payload.ReleaseDate)
movie.Year = movie.ReleaseDate.Year()
movie.Runtime, _ = strconv.Atoi(payload.Runtime)
movie.Rating, _ = strconv.Atoi(payload.Rating)
movie.MPAARating = payload.MPAARating
movie.UpdatedAt = time.Now()
err = app.models.Repository.UpdateMovie(movie, id)
if err != nil {
panic(err)
}
movieGenre, _ := app.models.Repository.GetMovieGenresById(id)
for _, mg := range *movieGenre {
mg.MovieID = movie.ID
mg.UpdatedAt = time.Now()
for _, v := range payload.GenreID {
mg.GenreID, _ = strconv.Atoi(v)
err := app.models.Repository.UpdateMovieGenre(&mg, id)
if err != nil {
panic(err)
}
}
}
ctx.JSON(http.StatusOK, &gin.H{
"ok": "response",
})
return
}
repository
func (MovieRepositoryImpl *MovieRepositoryImpl) UpdateMovie(movie *Movie, id int) error {
err := MovieRepositoryImpl.DB.Where("id = ?", id).Save(&movie).Error
if err != nil {
return err
}
return nil
}
func (MovieRepositoryImpl *MovieRepositoryImpl) UpdateMovieGenre(movieGenre *MovieGenre, id int) error {
err := MovieRepositoryImpl.DB.Where("movie_id = ?", id).Save(&movieGenre).Error
if err != nil {
return err
}
return nil
}
func (MovieRepositoryImpl *MovieRepositoryImpl) GetMovieGenresById(id int) (*[]MovieGenre, error) {
var movie []MovieGenre
err := MovieRepositoryImpl.DB.Where("movie_id = ?", id).Find(&movie).Error
if err != nil {
return nil, err
}
return &movie, nil
}
Model
type Movie struct {
ID int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Year int `json:"year"`
ReleaseDate time.Time `json:"release_date"`
Runtime int `json:"runtime"`
Rating int `json:"rating"`
MPAARating string `json:"mpaa_rating"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Genres []Genre `json:"genres" gorm:"many2many:movie_genres"`
MovieGenre []MovieGenre `json:"movie_genres" gorm:"many2many:movie_genres"`
}
type Genre struct {
ID int `json:"id"`
GenreName string `json:"name"`
Movies []Movie `json:"movies" gorm:"many2many:movie_genres"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type MovieGenre struct {
ID int `json:"id"`
MovieID int `json:"movie_id"`
Movie Movie `gorm:"foreignKey:MovieID"`
GenreID int `json:"genre_id"`
Genre Genre `gorm:"foreignKey:GenreID"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type MoviePayloadResponse struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Year string `json:"year"`
ReleaseDate string `json:"release_date"`
Runtime string `json:"runtime"`
Rating string `json:"rating"`
MPAARating string `json:"mpaa_rating"`
GenreID []string `json:"genre_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
My expectation is that the data genre_id that changes is only data whose value is not the same as the value in the payload, if it is the same then the data does not change, if it is different then it changes, and if the data in the payload exceeds the original genre data, then new data is automatically created. That's roughly what it looks like. Please help!!

Related

gorm [error] unsupported data type: &map[]

I want to create an API using map, but when in models I define map type, it error with message not support
This is Model
type Movie struct {
ID int `json:"id" validate:"number"`
Title string `json:"title"`
Description string `json:"description"`
Year int `json:"year"`
ReleaseDate time.Time `json:"release_date"`
Runtime int `json:"runtime"`
Rating int `json:"rating"`
MPAARating string `json:"mpaa_rating"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
MovieGenres map[int]string `json:"-" gorm:"many2many:movie_genres"`
}
type Genre struct {
ID int `json:"-"`
GenreName string `json:"genre_name"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type MovieGenre struct {
ID int `json:"id"`
MovieID int `json:"movie_id"`
GenreID int `json:"genre_id"`
Genre Genre `gorm:"foreignKey:GenreID"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
and this is the code to retrieve data by ID
func (MovieRepositoryImpl *MovieRepositoryImpl) GetMovieById(id int) (*Movie, error) {
var movie Movie
err := MovieRepositoryImpl.DB.First(&movie, id).Error
if err != nil {
return nil, err
}
var movie_genres MovieGenre
rows, err := MovieRepositoryImpl.DB.Model(&movie_genres).Preload(movie_genres.Genre.GenreName).Rows()
defer rows.Close()
genres := make(map[int]string)
for rows.Next() {
err := MovieRepositoryImpl.DB.ScanRows(rows, &movie_genres).Error
if err != nil {
panic(err)
}
genres[movie_genres.ID] = movie_genres.Genre.GenreName
}
movie.MovieGenres = genres
return &movie, nil
}
This is my Expect
You can change MovieGenres map[int]string to MovieGenres datatypes.JSONMap
https://gorm.io/docs/data_types.html
https://github.com/go-gorm/datatypes/blob/master/json_map.go

How to save data with association in gorm golang?

So, I want to save the movies and movie_genres data, but it always fails, which is stored in the database is the movies table as much as looping movie_genres, and the movie_genres column with the wrong movies ID. This is the result
movies table
movie_genres table
This the JSON result from frontend
{
"title":"Qui aut consectetur",
"release_date":"1974-11-25",
"runtime":"91",
"mpaa_rating":"PG",
"rating":"2",
"description":
"Anim dolor molestias",
"genre_id":["1","2"]
}
Below is my code on go
models
type Movie struct {
ID int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Year int `json:"year"`
ReleaseDate time.Time `json:"release_date"`
Runtime int `json:"runtime"`
Rating int `json:"rating"`
MPAARating string `json:"mpaa_rating"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Genres []Genre `json:"genres" gorm:"many2many:movie_genres"`
MovieGenre []MovieGenre `json:"movie_genres" gorm:"many2many:movie_genres"`
}
type Genre struct {
ID int `json:"id"`
GenreName string `json:"name"`
Movies []Movie `json:"movies" gorm:"many2many:movie_genres"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type MovieGenre struct {
ID int `json:"id"`
MovieID int `json:"movie_id"`
Movie Movie `gorm:"foreignKey:MovieID"`
GenreID int `json:"genre_id"`
Genre Genre `gorm:"foreignKey:GenreID"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
handler.go
func (app *Application) CreateMovies(ctx *gin.Context) {
var payload web.MoviePayloadResponse
if err := ctx.BindJSON(&payload); err != nil {
log.Println(err)
return
}
var movieGenre models.MovieGenre
movieGenre.Movie.ID, _ = strconv.Atoi(payload.ID)
movieGenre.Movie.Title = payload.Title
movieGenre.Movie.Description = payload.Description
movieGenre.Movie.ReleaseDate, _ = time.Parse("2006-01-02", payload.ReleaseDate)
movieGenre.Movie.Year = movieGenre.Movie.ReleaseDate.Year()
movieGenre.Movie.Runtime, _ = strconv.Atoi(payload.Runtime)
movieGenre.Movie.Rating, _ = strconv.Atoi(payload.Rating)
movieGenre.Movie.MPAARating = payload.MPAARating
movieGenre.Movie.CreatedAt = time.Now()
movieGenre.Movie.UpdatedAt = time.Now()
movieGenre.ID, _ = strconv.Atoi(payload.ID)
movieGenre.MovieID = movieGenre.Movie.ID
movieGenre.CreatedAt = time.Now()
movieGenre.UpdatedAt = time.Now()
err := app.models.Repository.CreateMovie(movieGenre.Movie)
if err != nil {
panic(err)
}
for _, v := range payload.GenreID {
movieGenre.GenreID, _ = strconv.Atoi(v)
err := app.models.Repository.CreateMovieGenres(movieGenre)
if err != nil {
panic(err)
}
}
ctx.JSON(http.StatusOK, &gin.H{
"ok": "response",
})
return
}
repository.go
func (MovieRepositoryImpl *MovieRepositoryImpl) CreateMovie(movie Movie) error {
MovieRepositoryImpl.DB.Create(&movie)
return nil
}
func (MovieRepositoryImpl *MovieRepositoryImpl) CreateMovieGenres(movieGenre MovieGenre) error {
MovieRepositoryImpl.DB.Create(&movieGenre)
return nil
}
payload
type MoviePayloadResponse struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Year string `json:"year"`
ReleaseDate string `json:"release_date"`
Runtime string `json:"runtime"`
Rating string `json:"rating"`
MPAARating string `json:"mpaa_rating"`
GenreID []string `json:"genre_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
The results that I expect are in the movies table, only 1 column is made, and in the movie_genres table store data movies as much as the genre inputted with the new movies ID. How to solve the problem?

i am working on supply chain DApps .here i want to add budget with ownername only not with the whole block

type Product struct {
ID string `json:"id"`
Name string `json:"name"`
Area string `json:"area"`
OwnerName string `json:"ownerName"`
Value int `json:"cost"`
Budget int `json:"budget"`
}
func (pc *ProductTransferSmartContract) AddProduct(ctx contractapi.TransactionContextInterface, id string, name string, area string, ownerName string, cost int, budget int) error {
productJSON, err := ctx.GetStub().GetState(id)
if err != nil {
return fmt.Errorf("Failed to read the data from world state", err)
}
if productJSON != nil {
return fmt.Errorf("the product %s already exists", id)
}
prop := Product{
ID: id,
Name: name,
Area: area,
OwnerName: ownerName,
Value: cost,
}
productBytes, err := json.Marshal(prop)
if err != nil {
return err
}
return ctx.GetStub().PutState(id, productBytes)
}
here i want to attach budget only with owner name so that if owner got changed i can change the budget too. Here i have written chaincode in golang.
If requirement is to restrict budget access only for owner, then private data collection can be used. Corresponding APIs are getPrivateData and putPrivateData.

Golang GORM Cascade Delete to Nested Fields

Main Model:
type Page struct {
ID int `gorm:"primarykey" json:"id"`
Title string `gorm:"unique;not null" json:"title"`
LocalizedPageTitles []LocalizedPageTitle `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"localizedPageTitles"`
Paragraphs []Paragraph `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"paragraphs"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
First Child:
type Paragraph struct {
ID uint `gorm:"primarykey" json:"id"`
Text string `gorm:"unique;not null" json:"text"`
PageID uint `json:"pageId"`
LocalizedParagraphs []LocalizedParagraph `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"localizedParagraphs"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
Second Child:
type LocalizedParagraph struct {
Localized
ID uint `gorm:"primarykey" json:"id"`
ParagraphID uint `json:"paragraphId"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
This is how I delete my page entity:
func (p PageRepositoryImpl) Delete(id int) error {
return p.db.Unscoped().Select(clause.Associations).Delete(&entity.Page{ID: id}).Error
}
Above function deletes Page and Paragraphs but how can I remove LocalizedParagraphs automatically?
By the way I'm using Sqlite.
dbmanager.go
func InitSQLite(filePath string) *gorm.DB {
database, err := gorm.Open(sqlite.Open(filePath), &gorm.Config{})
if err != nil {
fmt.Printf("Error:%v", err)
panic("Failed to connect database")
}
autoMigrateDB(database)
return database
}
func autoMigrateDB(db *gorm.DB) {
db.AutoMigrate(
&entity.Page{},
&entity.Paragraph{},
&entity.LocalizedPageTitle{},
&entity.LocalizedParagraph{},
)
}
I couldn't perform this action without delete hooks.
Select(clause.Associations) statement already take care of the one level associations:
func (p PageRepositoryImpl) Delete(id int) error {
return p.db.Unscoped().Select(clause.Associations).Delete(&entity.Page{ID: id}).Error
}
For nested associations, I used delete hook,
here is my solution:
func (page *Page) BeforeDelete(tx *gorm.DB) error {
paragraphs := make([]Paragraph, 0)
err := tx.Where("page_id = ?", page.ID).Find(&paragraphs).Error
if err != nil{
return err
}
ids := make([]int, 0, len(paragraphs))
for _, element := range paragraphs{
ids = append(ids, int(element.ID))
}
lps := make([]LocalizedParagraph,0)
err = tx.Where("paragraph_id IN ?", ids).Unscoped().Delete(&lps).Error
return err;
}

Receiving inconsistent data in Go api

I was learning the Go language and tested Google Cloud Functions with go + Google Firestore as the database.
While I was testing the response I got inconsistent responses.
I have used the json Marshaller to convert Firebase data to Json object to return from the API, this API is hosted in the Google Cloud Functions.
// Package p contains an HTTP Cloud Function.
package p
import (
"context"
"fmt"
"log"
"net/http"
s "strings"
"encoding/json"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
func HelloWorld(w http.ResponseWriter, r *http.Request) {
path := s.Replace(r.URL.Path, "/", "", -1)
ctx := context.Background()
client := createClient(ctx)
iter := client.Collection("profile").Where("publicUrl", "==", path).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
var publicDTO PublicDTO
var Profile Profile
doc.DataTo(&Profile)
publicDTO.Profile = Profile
b, err := json.Marshal(publicDTO)
if err != nil {
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
}
func createClient(ctx context.Context) *firestore.Client {
projectID := "projectId"
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
return client
}
type PublicDTO struct {
Profile Profile `json:"profile"`
}
type Profile struct {
Id string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
FullName string `json:"fullName"`
Email string `json:"email"`
ImageUrl string `json:"imageUrl"`
CoverPic string `json:"coverPic"`
Experience int `json:"experience"`
PhoneNumber string `json:"phoneNumber"`
Description string `json:"description"`
Address string `json:"address"`
State string `json:"state"`
Country string `json:"country"`
Dob map[string]string `json:"dob"`
Website string `json:"website"`
Reputation int `json:"reputation"`
MemberFrom map[string]int `json:"memberFrom"`
Title string `json:"title"`
Organization string `json:"organization"`
Status string `json:"status"`
Setup int `json:"setup"`
Social map[string]string `json:"social"`
PublicUrl string `json:"publicUrl"`
Language []string `json:"language"`
Interests []string `json:"interests"`
}
but each time the response i'm getting is inconsistent, some of the values are missing.
The solution i got after marshal and unmarshal, it works as expected.
package p
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
s "strings"
"time"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
var ctx context.Context
var client *firestore.Client
func PublicApi(w http.ResponseWriter, r *http.Request) {
path := s.Replace(r.URL.Path, "/", "", -1)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
newFsConfigBytes, _ := json.Marshal(getPublicDTO(path))
w.Write(newFsConfigBytes)
}
func getPublicDTO(path string) (publicDTO publicDTO) {
ctx = context.Background()
client = createClient()
profile, id := getProfiles(path)
publicDTO.Profile = profile
publicDTO.MilestoneDTOS = getMilestone(id)
return
}
func getProfiles(link string) (profile, string) {
var retVal profile
var id string
iter := client.Collection("profile").Where("publicUrl", "==", link).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
id = doc.Ref.ID
b, _ := json.Marshal(doc.Data())
json.Unmarshal(b, &retVal)
}
return retVal, id
}
func getMilestone(id string) []milestoneDTOS {
var retVal []milestoneDTOS
iter := client.Collection("milestone").Where("userId", "==", id).Documents(ctx)
for {
var milestoneDTO milestoneDTOS
doc, err := iter.Next()
if err == iterator.Done {
break
}
b, _ := json.Marshal(doc.Data())
err = json.Unmarshal(b, &milestoneDTO)
if err != nil {
fmt.Println(err)
}
retVal = append(retVal, milestoneDTO)
}
return retVal
}
func createClient() *firestore.Client {
projectID := "app_id_asda"
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
return client
}
type profile struct {
Address string `json:"address"`
City string `json:"city"`
Country string `json:"country"`
CoverPic string `json:"coverPic"`
CreatedBy string `json:"createdBy"`
CreatedDate int `json:"createdDate"`
Description string `json:"description"`
Dob int64 `json:"dob"`
Email string `json:"email"`
Enabled bool `json:"enabled"`
Experience int `json:"experience"`
FirstName string `json:"firstName"`
FullName string `json:"fullName"`
FullNameNoSpace string `json:"fullNameNoSpace"`
ImageURL string `json:"imageUrl"`
Interests []string `json:"interests"`
IsEnabled bool `json:"isEnabled"`
Language string `json:"language"`
LastModifiedDate int `json:"lastModifiedDate"`
LastName string `json:"lastName"`
LatLng string `json:"latLng"`
MemberFrom time.Time `json:"memberFrom"`
ObjectID string `json:"objectID"`
Organization string `json:"organization"`
PhoneNumber string `json:"phoneNumber"`
PlanID string `json:"planId"`
PublicURL string `json:"publicUrl"`
Reputation int `json:"reputation"`
Setup int `json:"setup"`
Social string `json:"social"`
State string `json:"state"`
Status string `json:"status"`
Title string `json:"title"`
Website string `json:"website"`
}
type milestoneDTOS struct {
Category string `json:"category"`
CreatedBy string `json:"createdBy"`
CreatedDate int `json:"createdDate"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
EndDate time.Time `json:"endDate"`
IsCurrentPosition bool `json:"isCurrentPosition"`
IsEnabled bool `json:"isEnabled"`
LastModifiedBy time.Time `json:"lastModifiedBy"`
LastModifiedDate int `json:"lastModifiedDate"`
ObjectID string `json:"objectID"`
Organization string `json:"organization"`
PictureURL string `json:"pictureURL"`
Profile string `json:"profile"`
Score float64 `json:"score"`
StartDate time.Time `json:"startDate"`
Tags []string `json:"tags"`
Title string `json:"title"`
URL string `json:"url"`
UserID string `json:"userId"`
}
type publicDTO struct {
Profile profile `json:"profile"`
MilestoneDTOS []milestoneDTOS `json:"milestoneDTOS"`
}

Resources