Skip to content

Instantly share code, notes, and snippets.

View yusufsholeh's full-sized avatar

yusufsholeh

View GitHub Profile
// Class ORM
type Mitra struct {
gorm.Model
Phone string `gorm:"type:varchar(50);not null;unique default:null"`
Email string `gorm:"type:varchar(50);not null;unique"`
Password string
}
type MitraRepository struct {
}
func TestMitraGetByPhoneNumber(t *testing.T) {
var mitraFieldNames = []string{"id", "phone", "email", "password"}
rows := sqlmock.NewRows(mitraFieldNames)
rows.AddRow("1", "0812345678", "abdul@gmail.com", "abcdefghi")
db, mock, _ := sqlmock.New()
gormDb, _ := gorm.Open("postgres", db)
gormDb.LogMode(true)
}
func TestMitraGetByPhoneNumber(t *testing.T) {
var mitraFieldNames = []string{"id", "phone", "email", "password"}
rows := sqlmock.NewRows(mitraFieldNames)
rows.AddRow("1", "0812345678", "abdul@gmail.com", "abcdefghi")
db, mock, _ := sqlmock.New()
gormDb, _ := gorm.Open("postgres", db)
gormDb.LogMode(true)
mock.ExpectQuery("^SELECT (.+) FROM \"mitras\".+$").WillReturnRows(rows)
models.SetDB(gormDb)
mitraRepo := new(MitraRepository)
package customercontrollers
import (
"encoding/json"
"net/http"
)
func writeEncoded(w http.ResponseWriter, data interface{}) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
type UserProfileService struct {
}
// class UserProfileServices implements this interface
type IUserProfileService interface {
GetUserProfile(userID int) (*models.User, error)
}
// class ORM: models.User
type User struct {
UserID int
Name string
Email string
Phone string
Address string
Password string
}
type UserProfileService struct {
UserRepository repositories.IUserRepository // masukkan interface repository ke dalam UserProfileServices
}
// class UserProfileServices implements this interface
type IUserProfileService interface {
GetUserProfile(userID int) (*models.User, error)
}
import "backend/repositories"
type UserProfileService struct {
UserRepository repositories.IUserRepository // put the dependency!!
}
// class UserProfileServices implements this interface
type IUserProfileService interface {
GetUserProfile(userID int) (*models.User, error)
}
func main() {
...
userRepository := new(UserRepository) // initiate an object
// initiate userProfileService object
userProfileService := UserProfileService{
UserRepository: userRepository // <--- inject !!
}
-- +migrate Up
-- SEQUENCE: public.customers_id_seq
CREATE SEQUENCE customers_id_seq;
-- Table: public.customers
CREATE TABLE customers
(
id integer NOT NULL DEFAULT nextval('customers_id_seq'::regclass),