This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type UserProfileService struct { | |
} | |
// class UserProfileServices implements this interface | |
type IUserProfileService interface { | |
GetUserProfile(userID int) (*models.User, error) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class ORM: models.User | |
type User struct { | |
UserID int | |
Name string | |
Email string | |
Phone string | |
Address string | |
Password string | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
... | |
userRepository := new(UserRepository) // initiate an object | |
// initiate userProfileService object | |
userProfileService := UserProfileService{ | |
UserRepository: userRepository // <--- inject !! | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- +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), |
OlderNewer