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
37 changes: 34 additions & 3 deletions api/externals/repository/teacher_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/NUTFes/FinanSu/api/externals/repository/abstract"
goqu "github.com/doug-martin/goqu/v9"
)

type teacherRepository struct {
Expand All @@ -30,7 +31,11 @@ func NewTeacherRepository(c db.Client, ac abstract.Crud) TeacherRepository {
}

func (t *teacherRepository) All(c context.Context) (*sql.Rows, error) {
query := "SELECT * FROM teachers WHERE is_deleted IS FALSE ORDER BY department_id ASC "
query, _, err := selectTeacherWithRoomQuery.
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

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

The All() query no longer filters out soft-deleted records (is_deleted). Add a Where(goqu.Ex{"teachers.is_deleted": false}) clause to the query builder so deleted teachers aren't returned.

Suggested change
query, _, err := selectTeacherWithRoomQuery.
query, _, err := selectTeacherWithRoomQuery.
Where(goqu.Ex{"teachers.is_deleted": false}).

Copilot uses AI. Check for mistakes.
ToSQL()
if err != nil {
return nil, err
}
return t.crud.Read(c, query)
}

Expand Down Expand Up @@ -62,7 +67,12 @@ func (t *teacherRepository) AllFundRegistered(c context.Context, year string) (*
}

func (t *teacherRepository) Find(c context.Context, id string) (*sql.Row, error) {
query := "SELECT * FROM teachers WHERE id = " + id
query, _, err := selectTeacherWithRoomQuery.
Where(goqu.Ex{"teachers.id": id}).
ToSQL()
if err != nil {
return nil, err
}
return t.crud.ReadByID(c, query)
}

Expand Down Expand Up @@ -125,7 +135,7 @@ func (t *teacherRepository) MultiDestroy(c context.Context, ids []int) error {
for index, id := range ids {
query += "id = " + strconv.Itoa(id)

if(index != len(ids)-1){
if index != len(ids)-1 {
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

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

Building the MultiDestroy query via string concatenation is error-prone and can be inefficient. Consider using a parameterized WHERE id IN (?) with Goqu for safety and performance.

Copilot uses AI. Check for mistakes.
query += " OR "
}

Expand All @@ -138,3 +148,24 @@ func (t *teacherRepository) MultiDestroy(c context.Context, ids []int) error {

return err
}

var selectTeacherWithRoomQuery = dialect.
From("room_teachers").
Join(
goqu.T("teachers"),
goqu.On(goqu.Ex{"room_teachers.teacher_id": goqu.I("teachers.id")}),
).
Join(
goqu.T("rooms"),
goqu.On(goqu.Ex{"room_teachers.room_id": goqu.I("rooms.id")}),
).
Select(
goqu.I("teachers.id"),
goqu.I("teachers.name"),
goqu.I("teachers.position"),
goqu.I("teachers.department_id"),
goqu.I("teachers.is_black"),
goqu.I("teachers.remark"),
goqu.I("teachers.is_deleted"),
goqu.I("rooms.room_name"),
)
121 changes: 44 additions & 77 deletions api/generated/openapi_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading