Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions core/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package core

import (
"context"
"log"

"github.com/tokopedia/code-review-workshop/database"
redislib "github.com/tokopedia/code-review-workshop/redis"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import lebih baik di rapihkan antara external dan internal code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
redislib "github.com/tokopedia/code-review-workshop/redis"
"github.com/tokopedia/code-review-workshop/database"
redislib "github.com/tokopedia/code-review-workshop/redis"
"github.com/tokopedia/sqlt"

"github.com/tokopedia/sqlt"
)

type PreparedStatements struct {
GetNilaiSiswaByID *sqlt.Stmtx
}

var (
db *sqlt.DB
stmt PreparedStatements
redis *redislib.RedisStore // redis module
)

const (
// Collection queries
QueryGetNilaiSiswaByID = `select nim, mapel_id, nilai, sekolah, kelas
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coalesce?

from nilai_siswa
where mapel_id = $1 and nim = $2;`
)

func Init() {
var err error
db, err = database.Get(database.PromoCatalog)
if err != nil {
log.Printf("Cannot connect to DB. %+v", err)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lebih baik log.Fatal kah? takutnya ketika servicenya jalan dan tidak bisa ke db jadi error

}

stmt = PreparedStatements{
GetNilaiSiswaByID: database.Preparex(context.Background(), db, QueryGetNilaiSiswaByID),
}

redis, err = redislib.Get(redislib.PromoCatalog)
if err != nil {
log.Fatal("Cannot connect to redis")
}
}
28 changes: 28 additions & 0 deletions core/nilai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core

import (
"context"

"github.com/tokopedia/code-review-workshop/errors"
"github.com/tokopedia/code-review-workshop/tracer"
)

type NilaiSiswa struct {
NIM int64 `json:"nim" db:"nim"`
MapelID int64 `json:"mapel_id" db:"mapel_id"`
Nilai int64 `json:"nili" db:"nili"`
Sekolah string `json:"sekolah" db:"sekolah"`
Kelas string `json:"kelas" db:"kelas"`
}

func GetNilaiSiswaByID(ctx context.Context, nim, mapel_id int64) (result []NilaiSiswa, err error) {
span, ctx := tracer.StartSpanFromContext(ctx)
defer span.Finish()

err = stmt.GetNilaiSiswaByID.Select(&result, mapel_id, nim)
if err != nil {
return result, errors.AddTrace(err)
}

return result, nil
}
25 changes: 25 additions & 0 deletions grpc/sekolah.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package grpc

import (
"context"

"github.com/tokopedia/code-review-workshop/errors"
mnilai "github.com/tokopedia/code-review-workshop/module/nilai"
pb "github.com/tokopedia/grpc/code-review-workshop/proto"
)

func (s *server) GetNilaiSiswa(ctx context.Context, req *pb.GetNilaiSiswaRequest) (resp *pb.GetNilaiSiswaResponse, err error) {

if req.sekolah == "" {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mungkin pake function Get() dari grpc untuk lebih aman

req.sekolah = "semua"
}

nilai, err := mnilai.GetNilaiSiswa(req.nim, req.sekolah, req.kelas, mapel_id)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

menggunakan function Get() dari GRPC

if err != nil {
return resp, errors.AddTrace(err)
}

resp = parseGetNilaiSiswa(nilai)

return resp, nil
}
54 changes: 54 additions & 0 deletions module/nilai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package module

import (
"context"

nilai "github.com/tokopedia/code-review-workshop/core/nilai"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import external code dipisah dengan import dari dalam service

"github.com/tokopedia/tokopoints/errors"
"github.com/tokopedia/tokopoints/tracer"
)

func GetNilaiSiswa(ctx context.Context, nim int64, sekolah, kelas string, mapel_id int64) (resp GetNilaiSiswaResponse, err error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameter sebaiknya di bikin sebuah struct yang baru

span, ctx := tracer.StartSpanFromContext(ctx)
defer span.Finish()

if nim != 0 && mapel_id != 0 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bisa di buat switch case dan di dalamnya masuk2in
-GetNilaiSiswaByID
-GetNilaiSiswaBySekolah
-GetNilaiSiswaSemuaSekolah

lalu baru di luar untuk proses

  • GetDetailSekolah dan mappingGetNilaiSiswa

nilai, err := nilai.GetNilaiSiswaByID(ctx, nim, mapel_id)
if err != nil {
return resp, errors.AddTrace(err)
}

detailSekolah, err := nilai.GetDetailSekolah(ctx, nilai.Sekolah[0])
if err != nil {
return resp, errors.AddTrace(err)
}

resp = mappingGetNilaiSiswa(nilai, detailSekolah)
} else if sekolah != "semua" {
nilai, err := nilai.GetNilaiSiswaBySekolah(ctx, sekolah)
if err != nil {
return resp, errors.AddTrace(err)
}

detailSekolah, err := nilai.GetDetailSekolah(ctx, nilai.Sekolah[0])
if err != nil {
return resp, errors.AddTrace(err)
}

resp = mappingGetNilaiSiswa(nilai, detailSekolah)
} else if sekolah == "semua" {
nilai, err := nilai.GetNilaiSiswaSemuaSekolah(ctx)
if err != nil {
return resp, errors.AddTrace(err)
}

detailSekolah, err := nilai.GetDetailSekolah(ctx, nilai.Sekolah[0])
if err != nil {
return resp, errors.AddTrace(err)
}

resp = mappingGetNilaiSiswa(nilai, detailSekolah)
}

return resp, nil
}