Skip to content

Commit

Permalink
fix: handle some code review issues(except for teacher, teaching_plan…
Browse files Browse the repository at this point in the history
…_course_po)
  • Loading branch information
Ayanami1314 committed Aug 10, 2024
1 parent b035175 commit fb872aa
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 198 deletions.
38 changes: 9 additions & 29 deletions handler/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handler

import (
"fmt"
"jcourse_go/middleware"
"jcourse_go/model/converter"
"jcourse_go/model/domain"
"jcourse_go/model/dto"
Expand All @@ -18,15 +17,15 @@ func GetTrainingPlanListHandler(c *gin.Context) {
c.JSON(http.StatusBadRequest, dto.BaseResponse{Message: "参数错误"})
return
}
Filter := domain.TrainingPlanFilter{
filter := domain.TrainingPlanFilter{
Page: int64(request.Page),
PageSize: int64(request.PageSize),
}
TrainingPlanList, err := service.SearchTrainingPlanList(c, Filter)
trainingPlanList, err := service.SearchTrainingPlanList(c, filter)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "内部错误。"})
}
data := converter.ConvertTrainingPlanDomainListToDTO(TrainingPlanList)
data := converter.ConvertTrainingPlanDomainListToDTO(trainingPlanList)
response := dto.TrainingPlanListResponse{
Page: int64(request.Page),
PageSize: int64(request.PageSize),
Expand All @@ -42,12 +41,12 @@ func GetTrainingPlanHandler(c *gin.Context) {
c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "参数错误"})
return
}
TrainingPlan, err := service.GetTrainingPlanDetail(c, request.TrainingPlanID)
trainingPlan, err := service.GetTrainingPlanDetail(c, request.TrainingPlanID)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "内部错误。"})
return
}
response := converter.ConvertTrainingPlanDomainToDTO(*TrainingPlan)
response := converter.ConvertTrainingPlanDomainToDTO(*trainingPlan)
c.JSON(http.StatusOK, response)
}

Expand All @@ -58,19 +57,19 @@ func SearchTrainingPlanHandler(c *gin.Context) {
return
}

Filter := domain.TrainingPlanFilter{
filter := domain.TrainingPlanFilter{
Major: request.MajorName,
EntryYear: fmt.Sprintf("%d", request.EntryYear),
Department: request.Department,
Page: int64(request.Page),
PageSize: int64(request.PageSize),
}
TrainingPlanList, err := service.SearchTrainingPlanList(c, Filter)
count := service.GetTrainingPlanCount(c, Filter)
trainingPlanList, err := service.SearchTrainingPlanList(c, filter)
count := service.GetTrainingPlanCount(c, filter)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "内部错误。"})
}
data := converter.ConvertTrainingPlanDomainListToDTO(TrainingPlanList)
data := converter.ConvertTrainingPlanDomainListToDTO(trainingPlanList)
response := dto.TrainingPlanListResponse{
Page: int64(request.Page),
PageSize: int64(len(data)),
Expand All @@ -79,22 +78,3 @@ func SearchTrainingPlanHandler(c *gin.Context) {
}
c.JSON(http.StatusOK, response)
}

// ATTENTION: without test now
func RateTrainingPlanHandler(c *gin.Context) {
var request dto.RateTrainingPlanRequest
userId := middleware.GetUser(c).ID
if err := c.ShouldBind(&request); err != nil {
c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "参数错误"})
return
}
err := service.RateTrainingPlan(c, userId, request.TrainingPlanID, request.Rate)
// HINT: 底层upsert,不会出现重复插入错误
if err != nil {
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "内部错误。"})
println(err)
return
}
response := dto.BaseResponse{Message: "评分成功"}
c.JSON(http.StatusOK, response)
}
2 changes: 1 addition & 1 deletion handler/trainingplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func prettyJsonLog(w *httptest.ResponseRecorder) {
}
func baseConfig() (*httptest.ResponseRecorder, *gin.Engine) {
gin.SetMode(gin.TestMode)
os.Setenv("ROOT_DIR", "/home/ayanmi/code/jcourse/jcourse_go")
os.Setenv("ROOT_DIR", "../")
rootDir := os.Getenv("ROOT_DIR")
_ = godotenv.Load(rootDir + "/.env")
dal.InitDBClient()
Expand Down
35 changes: 0 additions & 35 deletions model/converter/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,3 @@ func ConvertTrainingPlanCoursePOToDomain(coursePO po.TrainingPlanCoursePO, baseC
Department: coursePO.Department,
}
}

func ConvertTrainingPlanRatePOToDomain(ratePO po.TrainingPlanRatePO) domain.TrainingPlanRate {
return domain.TrainingPlanRate{
TrainingPlanID: ratePO.TrainingPlanID,
Rate: ratePO.Rate,
UserID: ratePO.UserID,
}
}

func ConvertTrainingPlanRateDomainToDTO(rate domain.TrainingPlanRate) dto.TrainingPlanRateDTO {
return dto.TrainingPlanRateDTO{
TrainingPlanID: rate.TrainingPlanID,
Rate: rate.Rate,
UserID: rate.UserID,
}
}

func ConvertTrainingPlanRateInfoDomainToDTO(rateInfo domain.TrainingPlanRateInfo) dto.TrainingPlanRateInfoDTO {
rateMap := map[int64][]domain.TrainingPlanRate{}
for _, r := range rateInfo.Rates {
rateMap[r.Rate] = append(rateMap[r.Rate], r)
}
rateDist := make([]dto.TrainingPlanRateItem, 0)
for k, v := range rateMap {
rateDist = append(rateDist, dto.TrainingPlanRateItem{
Rate: float64(k),
Count: int64(len(v)),
})
}
return dto.TrainingPlanRateInfoDTO{
Avg: rateInfo.Avg,
Count: rateInfo.Count,
RateDist: rateDist,
}
}
21 changes: 0 additions & 21 deletions model/dto/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ type TrainingPlanCourseDTO struct {
Department string `json:"department"`
}

// rate trainingplan post request(need session to confirm userID)
type RateTrainingPlanRequest struct {
TrainingPlanID int64 `form:"training_plan_id" binding:"required"`
Rate int64 `form:"rate" binding:"required"`
}
type RateTrainingPlanResponse = BaseResponse
type TrainingPlanRateItem struct {
Rate float64 `json:"rate"`
Count int64 `json:"count"`
}
type TrainingPlanRateDTO struct {
TrainingPlanID int64 `json:"training_plan_id"`
UserID int64 `json:"user_id"`
Rate int64 `json:"rate"`
}
type TrainingPlanRateInfoDTO struct {
Avg float64 `json:"avg"`
Count int64 `json:"count"`
RateDist []TrainingPlanRateItem `json:"rate_dist"`
}
type TrainingPlanListItemDTO struct {
ID int64 `json:"id"`
Code string `json:"code"`
Expand All @@ -41,7 +21,6 @@ type TrainingPlanListItemDTO struct {
TotalYear int64 `json:"total_year"`
Grade float32 `json:"grade"`
Degree string `json:"degree"`
RateInfo TrainingPlanRateInfoDTO `json:"rate_info"`
Courses []TrainingPlanCourseDTO `json:"courses"`
}

Expand Down
22 changes: 4 additions & 18 deletions model/po/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ func (po *TrainingPlanPO) TableName() string {

type TrainingPlanCoursePO struct {
gorm.Model
CourseID int64 `gorm:"index;index:uniq_training_plan_course,unique"`
TrainingPlanID int64 `gorm:"index;index:uniq_training_plan_course,unique"`
CourseID int64 `gorm:"index;index:uniq_training_plan_course,unique"`
TrainingPlanID int64 `gorm:"index;index:uniq_training_plan_course,unique"`
//SuggestTime string `gorm:"index;index:uniq_training_plan_course,unique"`
// 推荐修读时间:学年+学期,如 2023-2024-2
SuggestYear int64 `gorm:"index;index:uniq_training_plan_course,unique"`
SuggestSemester int64 `gorm:"index;index:uniq_training_plan_course,unique"`
Department string `gorm:"index;"`
Expand All @@ -93,22 +95,6 @@ func (po *TrainingPlanCoursePO) TableName() string {
return "training_plan_courses"
}

type TrainingPlanRatePO struct {
gorm.Model
UserID int64 `gorm:"index;index:uniq_training_plan_rate,unique"`
TrainingPlanID int64 `gorm:"index;index:uniq_training_plan_rate,unique"`
Rate int64 `gorm:"index"`
}
type TrainingPlanRateInfoPO struct {
Average float64
Count int64
Rates []TrainingPlanRatePO
}

func (po *TrainingPlanRatePO) TableName() string {
return "training_plan_rates"
}

type CourseReviewInfo struct {
CourseID int64
Average float64
Expand Down
74 changes: 0 additions & 74 deletions repository/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package repository

import (
"context"
"gorm.io/gorm/clause"
"jcourse_go/dal"
"jcourse_go/model/po"

Expand Down Expand Up @@ -210,76 +209,3 @@ func (t *TrainingPlanQuery) WithIDs(trainingPlanIDs []int64) DBOption {
return db.Where("id IN ?", trainingPlanIDs)
}
}

type ITrainingPlanRateQuery interface {
GetTrainingPlanRateInfo(ctx context.Context, trainingPlanID int64) (*po.TrainingPlanRateInfoPO, error)
GetTrainingPlanRateList(ctx context.Context, ops ...DBOption) ([]po.TrainingPlanPO, error)
CreateTrainingPlanRate(ctx context.Context, rate *po.TrainingPlanRatePO) error
optionDB(ctx context.Context, opts ...DBOption) *gorm.DB
WithUserID(id int64) DBOption
WithTrainingPlanID(id int64) DBOption
}
type TrainingPlanRateQuery struct {
db *gorm.DB
}

func NewTrainingPlanRateQuery() ITrainingPlanRateQuery {
return &TrainingPlanRateQuery{db: dal.GetDBClient()}
}
func (t *TrainingPlanRateQuery) CreateTrainingPlanRate(ctx context.Context, rate *po.TrainingPlanRatePO) error {
db := t.optionDB(ctx)
err := db.Debug().Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "training_plan_id"}, {Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"rate"}),
}).Create(rate).Error
return err
}

func (t *TrainingPlanRateQuery) WithTrainingPlanID(id int64) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("training_plan_id = ?", id)
}
}
func (t *TrainingPlanRateQuery) WithUserID(id int64) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("user_id = ?", id)
}
}
func (t *TrainingPlanRateQuery) optionDB(ctx context.Context, ops ...DBOption) *gorm.DB {
db := t.db.Model(&po.TrainingPlanRatePO{}).WithContext(ctx)
for _, opt := range ops {
db = opt(db)
}
return db
}
func (t *TrainingPlanRateQuery) GetTrainingPlanRateInfo(ctx context.Context, trainingPlanID int64) (*po.TrainingPlanRateInfoPO, error) {
db := t.optionDB(ctx)
rateInfo := po.TrainingPlanRateInfoPO{}
rates := make([]po.TrainingPlanRatePO, 0)
result := db.Debug().Where("training_plan_id = ?", trainingPlanID).Find(&rates)
if result.Error != nil {
return nil, result.Error
}
rateInfo.Count = int64(len(rates))
if rateInfo.Count == 0 {
return &rateInfo, nil
}
var sum int64 = 0
for _, rate := range rates {
sum += rate.Rate
}

rateInfo.Average = float64(sum) / float64(rateInfo.Count)
rateInfo.Rates = rates
return &rateInfo, nil
}

func (t *TrainingPlanRateQuery) GetTrainingPlanRateList(ctx context.Context, ops ...DBOption) ([]po.TrainingPlanPO, error) {
db := t.optionDB(ctx, ops...)
trainingPlans := make([]po.TrainingPlanPO, 0)
result := db.Debug().Find(&trainingPlans)
if result.Error != nil {
return nil, result.Error
}
return trainingPlans, nil
}
11 changes: 0 additions & 11 deletions service/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"jcourse_go/model/converter"
"jcourse_go/model/domain"
"jcourse_go/model/po"
"jcourse_go/repository"
)

Expand Down Expand Up @@ -107,13 +106,3 @@ func GetTrainingPlanListByIDs(ctx context.Context, trainingPlanIDs []int64) (map
}
return domainTrainingPlans, nil
}

func RateTrainingPlan(ctx context.Context, userID int64, trainingPlanID int64, rate int64) error {
trainingPlanRateQuery := repository.NewTrainingPlanRateQuery()
trainingPlanRatePO := &po.TrainingPlanRatePO{
TrainingPlanID: trainingPlanID,
UserID: userID,
Rate: rate,
}
return trainingPlanRateQuery.CreateTrainingPlanRate(ctx, trainingPlanRatePO)
}
10 changes: 6 additions & 4 deletions util/selenium-get/load2db.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func LoadedCourse2PO(course LoadedCourse) (po.BaseCoursePO, po.TrainingPlanCours
}
}
func Line2Course(line string) LoadedCourse {
// AD102,素描(1),4.0,2018-2019,1,
// e.g. CH118,初级汉语精读(1),8.0,2018-2019,1,巴黎卓越工程师学院
meta := strings.Split(line, ",")
if len(meta) != 6 {
panic("Invalid line2course: " + line)
Expand Down Expand Up @@ -93,7 +93,7 @@ func Lines2TrainingPlan(lines []string) LoadedTrainingPlan {
}
func TrainingPlan2PO(plan LoadedTrainingPlan) po.TrainingPlanPO {
return po.TrainingPlanPO{
Degree: "",
Degree: plan.Degree,
Major: plan.Name,
Department: plan.Department,
EntryYear: strconv.Itoa(plan.EntryYear),
Expand Down Expand Up @@ -224,10 +224,12 @@ func SaveTeacher(teachers []LoadedTeacher, db *gorm.DB) {
}
return
}
teacherPOs := make([]po.TeacherPO, 0)
for _, teacher := range teachers {
teacherPO := Teacher2PO(teacher)
db.Save(&teacherPO)
teacherPOs = append(teacherPOs, Teacher2PO(teacher))
}
db.CreateInBatches(teacherPOs, 100)

}
func LoadTeacherProfile2DB(from string, db *gorm.DB) {
teachers := LoadTeacherProfiles(from)
Expand Down
5 changes: 0 additions & 5 deletions util/selenium-get/teacher-profile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import os
import requests
from copy import deepcopy
import gzip
import json
import pprint
import time
from pypinyin import lazy_pinyin
from typing import List
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from argparse import ArgumentParser

driver = webdriver.Chrome()
Expand Down

0 comments on commit fb872aa

Please sign in to comment.