Coder Social home page Coder Social logo

advance-golang-result-new's Introduction

Table of Contents


Database Preparation for Relation

Database Design

Database Design

Models

  • Create user.go file inside models folder, and write this below code

    File: models/user.go

    package models
    
    import "time"
    
    type User struct {
      ID        int       `json:"id"`
      Name      string    `json:"name" gorm:"type: varchar(255)"`
      Email     string    `json:"email" gorm:"type: varchar(255)"`
      Password  string    `json:"-" gorm:"type: varchar(255)"`
      CreatedAt time.Time `json:"-"`
      UpdatedAt time.Time `json:"-"`
    }
    
    type UsersProfileResponse struct {
      ID   int    `json:"id"`
      Name string `json:"name"`
    }
    
    func (UsersProfileResponse) TableName() string {
      return "users"
    }
  • Create profile.go file inside models folder, and write this below code

    File: models/profile.go

    package models
    
    import "time"
    
    type Profile struct {
      ID        int                  `json:"id" gorm:"primary_key:auto_increment"`
      Phone     string               `json:"phone" gorm:"type: varchar(255)"`
      Gender    string               `json:"gender" gorm:"type: varchar(255)"`
      Address   string               `json:"address" gorm:"type: text"`
      UserID    int                  `json:"user_id"`
      User      UsersProfileResponse `json:"user"`
      CreatedAt time.Time            `json:"-"`
      UpdatedAt time.Time            `json:"-"`
    }
    
    // for association relation with another table (user)
    type ProfileResponse struct {
      Phone   string `json:"phone"`
      Gender  string `json:"gender"`
      Address string `json:"address"`
      UserID  int    `json:"-"`
    }
    
    func (ProfileResponse) TableName() string {
      return "profiles"
    }
  • Create product.go file inside models folder, and write this below code

    File: models/product.go

    package models
    
    import "time"
    
    type Product struct {
      ID         int                  `json:"id" gorm:"primary_key:auto_increment"`
      Name       string               `json:"name" form:"name" gorm:"type: varchar(255)"`
      Desc       string               `json:"desc" gorm:"type:text" form:"desc"`
      Price      int                  `json:"price" form:"price" gorm:"type: int"`
      Image      string               `json:"image" form:"image" gorm:"type: varchar(255)"`
      Qty        int                  `json:"qty" form:"qty"`
      UserID     int                  `json:"user_id" form:"user_id"`
      User       UsersProfileResponse `json:"user"`
      Category   []Category           `json:"category" gorm:"many2many:product_categories"`
      CategoryID []int                `json:"category_id" form:"category_id" gorm:"-"`
      CreatedAt  time.Time            `json:"-"`
      UpdatedAt  time.Time            `json:"-"`
    }
    
    type ProductResponse struct {
      ID         int                  `json:"id"`
      Name       string               `json:"name"`
      Desc       string               `json:"desc"`
      Price      int                  `json:"price"`
      Image      string               `json:"image"`
      Qty        int                  `json:"qty"`
      UserID     int                  `json:"-"`
      User       UsersProfileResponse `json:"user"`
      Category   []Category           `json:"category" gorm:"many2many:product_categories"`
      CategoryID []int                `json:"category_id" form:"category_id" gorm:"-"`
    }
    
    type ProductUserResponse struct {
      ID     int    `json:"id"`
      Name   string `json:"name"`
      Desc   string `json:"desc"`
      Price  int    `json:"price"`
      Image  string `json:"image"`
      Qty    int    `json:"qty"`
      UserID int    `json:"-"`
    }
    
    func (ProductResponse) TableName() string {
      return "products"
    }
    
    func (ProductUserResponse) TableName() string {
      return "products"
    }
  • Create category.go file inside models folder, and write this below code

    File: models/category.go

    package models
    
    import "time"
    
    type Category struct {
      ID        int       `json:"id" gorm:"primary_key:auto_increment"`
      Name      string    `json:"name"`
      CreatedAt time.Time `json:"-"`
      UpdatedAt time.Time `json:"-"`
    }
  • Create transaction.go file inside models folder, and write this below code

    File: models/transaction.go

    package models
    
    import "time"
    
    type Transaction struct {
      ID        int                  `json:"id" gorm:"primary_key:auto_increment"`
      ProductID int                  `json:"product_id"`
      Product   ProductResponse      `json:"product"`
      BuyerID   int                  `json:"buyer_id"`
      Buyer     UsersProfileResponse `json:"buyer"`
      SellerID  int                  `json:"seller_id"`
      Seller    UsersProfileResponse `json:"seller"`
      Price     int                  `json:"price"`
      Status    string               `json:"status"  gorm:"type:varchar(25)"`
      CreatedAt time.Time            `json:"-"`
      UpdatedAt time.Time            `json:"-"`
    }

Data Transfer Object (DTO)

  • Create auth folder, inside it Create auth_request.go and write this below code

    File: dto/auth/auth_request.go

    package authdto
    
    type AuthRequest struct {
      Name     string `gorm:"type: varchar(255)" json:"name"`
      Email    string `gorm:"type: varchar(255)" json:"email"`
      Password string `gorm:"type: varchar(255)" json:"password"`
    }
  • Create product folder, inside it Create product_request.go and write this below code

    File: dto/product/product_request.go

    package productdto
    
    type ProductRequest struct {
      Name       string `json:"name" form:"name" gorm:"type: varchar(255)"`
      Desc       string `json:"desc" gorm:"type:text" form:"desc"`
      Price      int    `json:"price" form:"price" gorm:"type: int"`
      Image      string `json:"image" form:"image" gorm:"type: varchar(255)"`
      Qty        int    `json:"qty" form:"qty" gorm:"type: int"`
      UserID     int    `json:"user_id" gorm:"type: int"`
      CategoryID int    `json:"category_id" form:"category_id" gorm:"type: int"`
    }
  • Create profile folder, inside it Create profile_request.go and write this below code

    File: dto/profile/profile_request.go

    package profiledto
    
    import "dumbmerch/models"
    
    type ProfileResponse struct {
      ID      int                         `json:"id" gorm:"primary_key:auto_increment"`
      Phone   string                      `json:"phone" gorm:"type: varchar(255)"`
      Gender  string                      `json:"gender" gorm:"type: varchar(255)"`
      Address string                      `json:"address" gorm:"type: text"`
      UserID  int                         `json:"user_id"`
      User    models.UsersProfileResponse `json:"user"`
    }

Modify Repository

  • Inside repositories folder, create repository.go file, and write this below code

    File: repositories/repository.go

    package repositories
    
    import "gorm.io/gorm"
    
    type repository struct {
      db *gorm.DB
    }

    *repository struct move from user.go file

advance-golang-result-new's People

Contributors

suryaelidanto avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.