DEV Community

loading...

Golang, Vuejs, Tailwinds classified ads models and database

bajro17 profile image Bajro ・3 min read

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 model

import "gorm.io/gorm"

// Image struct
type Image struct {
    gorm.Model
    URL       string `gorm:"not null" json:"url"`
        ProductID uint `gorm:"not null" json:"product_id"`
}
Enter fullscreen mode Exit fullscreen mode

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 model

import "gorm.io/gorm"

// Product struct
type 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
}


Enter fullscreen mode Exit fullscreen mode

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 model

import "gorm.io/gorm"

// User struct
type 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

}
Enter fullscreen mode Exit fullscreen mode

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 model

import "gorm.io/gorm"

// Category struct
type 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 

}
Enter fullscreen mode Exit fullscreen mode

Some of the other structs we made city and state.
I will just paste them here because they are not something special.

package model

import "gorm.io/gorm"

// State struct
type State struct {
    gorm.Model
    Name        uint `gorm:"not null" json:"Name"`
    Cities      []City
}
Enter fullscreen mode Exit fullscreen mode
package model

import "gorm.io/gorm"

// City struct
type City struct {
    gorm.Model
    Name       string `gorm:"not null" json:"Name"`
    StateID  uint  `gorm:"not null" json:"StateID"`
}
Enter fullscreen mode Exit fullscreen mode

Also, don't forget to change in database/connect.go line 28 from

DB.AutoMigrate(&model.Product{}, &model.User{})
Enter fullscreen mode Exit fullscreen mode

to

DB.AutoMigrate(&model.Category{},&model.Product{}, &model.User{}, &model.State{},&model.City{},&model.Image{})
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (0)

Forem Open with the Forem app