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 model

import “gorm.io/gorm”

// Im…


This content originally appeared on DEV Community and was authored by Bajro

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"`
}

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
}


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

}

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 

}

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
}
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"`
}

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.


This content originally appeared on DEV Community and was authored by Bajro


Print Share Comment Cite Upload Translate Updates
APA

Bajro | Sciencx (2021-04-19T21:24:20+00:00) Golang, Vuejs, Tailwinds classified ads models and database. Retrieved from https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/

MLA
" » Golang, Vuejs, Tailwinds classified ads models and database." Bajro | Sciencx - Monday April 19, 2021, https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/
HARVARD
Bajro | Sciencx Monday April 19, 2021 » Golang, Vuejs, Tailwinds classified ads models and database., viewed ,<https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/>
VANCOUVER
Bajro | Sciencx - » Golang, Vuejs, Tailwinds classified ads models and database. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/
CHICAGO
" » Golang, Vuejs, Tailwinds classified ads models and database." Bajro | Sciencx - Accessed . https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/
IEEE
" » Golang, Vuejs, Tailwinds classified ads models and database." Bajro | Sciencx [Online]. Available: https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/. [Accessed: ]
rf:citation
» Golang, Vuejs, Tailwinds classified ads models and database | Bajro | Sciencx | https://www.scien.cx/2021/04/19/golang-vuejs-tailwinds-classified-ads-models-and-database/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.