-
Notifications
You must be signed in to change notification settings - Fork 22
workshop content #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
workshop content #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| "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 | ||
| 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) | ||
| } | ||
|
|
||
| stmt = PreparedStatements{ | ||
| GetNilaiSiswaByID: database.Preparex(context.Background(), db, QueryGetNilaiSiswaByID), | ||
| } | ||
|
|
||
| redis, err = redislib.Get(redislib.PromoCatalog) | ||
| if err != nil { | ||
| log.Fatal("Cannot connect to redis") | ||
| } | ||
| } | ||
| 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"` | ||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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) { | ||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead mapel_id mngkn lbh oke mapelID |
||||||
| 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 | ||||||
| } | ||||||
| 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 == "" { | ||
| req.sekolah = "semua" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "semua" dijadiin const |
||
| } | ||
|
|
||
| nilai, err := mnilai.GetNilaiSiswa(req.nim, req.sekolah, req.kelas, mapel_id) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mapel_id ga ada di parameter dapet dari mana |
||
| if err != nil { | ||
| return resp, errors.AddTrace(err) | ||
| } | ||
|
|
||
| resp = parseGetNilaiSiswa(nilai) | ||
|
|
||
| return resp, nil | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lngsng return parseGetNilaiSiswa(nilai), nil |
||
| } | ||
| 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" | ||||||||||||||||||||
| "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) { | ||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bisa dijadiin struct aja paramny biar kalo ada tmbhan lbh mudah
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mape_id jadi mapelID |
||||||||||||||||||||
| span, ctx := tracer.StartSpanFromContext(ctx) | ||||||||||||||||||||
| defer span.Finish() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if nim != 0 && mapel_id != 0 { | ||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ini if nya dijadiin switch case aja
Suggested change
|
||||||||||||||||||||
| nilai, err := nilai.GetNilaiSiswaByID(ctx, nim, mapel_id) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return resp, errors.AddTrace(err) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| detailSekolah, err := nilai.GetDetailSekolah(ctx, nilai.Sekolah[0]) | ||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ini funcnya dikeluarin aja di luar if else |
||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return resp, errors.AddTrace(err) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| resp = mappingGetNilaiSiswa(nilai, detailSekolah) | ||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ini juga dikeluarin |
||||||||||||||||||||
| } 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) | ||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. functionny blm ada nih |
||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return resp, errors.AddTrace(err) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| detailSekolah, err := nilai.GetDetailSekolah(ctx, nilai.Sekolah[0]) | ||||||||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ini jg blm ada functionnya |
||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return resp, errors.AddTrace(err) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| resp = mappingGetNilaiSiswa(nilai, detailSekolah) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return resp, nil | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dirapiin query nya jadi lbh rapi atau sejajar