An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
Golang, Vuejs, Tailwinds classified ads models and database
Welcome back
I'm sorry because I was not able to continue my series earlier. I was also thinking of the best way how to make it.
Let's start with a simple Image struct it will look like this.
package modelimport "gorm.io/gorm"// Image structtype Image struct { gorm.Model URL string `gorm:"not null" json:"url"` ProductID uint `gorm:"not null" json:"product_id"`}
I need to mention each of the structs I made will be saved in the file with the same name, the previous struct is in image.go and all will exist in the model folder.
For Image struct, we can also without gorm.Model but I decide to put it in every model.
We need only the URL field because we will just save our image and URL to it.
Our next model will be the product model, it is the most complicated and probably this is not a final form I guess I will add few more fields to it.
package modelimport "gorm.io/gorm"// Product structtype Product struct { gorm.Model Title string `gorm:"not null" json:"title"` Description string `gorm:"not null" json:"description"` Quantity uint `gorm:"not null" json:"quantity"` Price int `gorm:"not null" json:"price"` Used bool `gorm:"not null" json:"Used"` Sold bool `gorm:"default:false" json:"sold"` UserID uint `gorm:"not null" json:"user_id"` CategoryID uint `gorm:"not null" json:"category_id"` Images []Image}
Here Title, Description, Quantity, Price I guess you already know for what it will be used. The used field is to tell if the product is 'used' or 'new'.
The sold field we use to know if the item is sold we can also do soft delete and avoid this field but to make it clear I will use it.
UserID and CategoryID fields are used to refer to the id of the user and category id gorm know automatically what we want and it makes reference for us.
I make also some changes in the User struct and now it looks like this
package modelimport "gorm.io/gorm"// User structtype User struct { gorm.Model Username string `gorm:"unique_index;not null" json:"username"` Email string `gorm:"unique_index;not null" json:"email"` Password string `gorm:"not null" json:"password"` FirstName string `gorm:"null" json:"first_name"` LastName string `gorm:"null" json:"last_name"` CityID uint `gorm:"null" json:"city_id"` PostalCode string `gorm:"null" json:"postal_code"` Address string `gorm:"null" json:"address"` IsShop bool `gorm:"default:false" json:"is_shop"` IsActive bool `gorm:"default:false" json:"is_active"` Image string `gorm:"null" json:"image"` Points int `gorm:"default:0" json:"points"` Products []Product}
Most of the fields are self-explanatory except IsShop, Image, Points.
IsShop field will be used to check if a user buys a premium account only premium accounts can open shop and extra features.
The image doesn't refer to Image struct because the user needs only one image and we will just save the URL to the user table.
Points are just some points that user will earn by buying and selling stuff and with it get some discounts.
We made also a category struct all of our products will belong to one of them. The category is self-referencing all is described in gorm documentation we will later check if parent_id is null and see the children categories.
package modelimport "gorm.io/gorm"// Category structtype Category struct { gorm.Model Name string `gorm:"not null" json:"Name"` Icon string `gorm:"null" json:"Icon"` Price uint `gorm:"default:0" json:"Price"` Expire uint `gorm:"default:30" json:"Expire"` ParentID *uint `gorm:"null" json:"parent_id"` Parent []Category `gorm:"foreignkey:ParentID"` Products []Product }
Some of the other structs we made city and state.
I will just paste them here because they are not something special.
package modelimport "gorm.io/gorm"// State structtype State struct { gorm.Model Name uint `gorm:"not null" json:"Name"` Cities []City}
package modelimport "gorm.io/gorm"// City structtype City struct { gorm.Model Name string `gorm:"not null" json:"Name"` StateID uint `gorm:"not null" json:"StateID"`}
Also, don't forget to change in database/connect.go line 28 from
DB.AutoMigrate(&model.Product{}, &model.User{})
to
DB.AutoMigrate(&model.Category{},&model.Product{}, &model.User{}, &model.State{},&model.City{},&model.Image{})
If you have any question or suggestions feel free to write them in the comments also my English is a little broken if there are some grammar mistakes please tell me so I can fix them.
I will write some logic in the next tutorial and maybe start with VueJS and Tailwind after this.
Original Link: https://dev.to/bajro17/golang-vuejs-tailwinds-classified-ads-models-and-database-25ib
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To