@@ -2,7 +2,6 @@ package db
22
33import (
44 "context"
5- "sort"
65
76 set "github.com/deckarep/golang-set/v2"
87 "github.com/obcode/plexams.go/graph/model"
@@ -37,88 +36,103 @@ func (db *DB) StudentRegsPerStudentPlanned(ctx context.Context) ([]*model.Studen
3736 return studentRegs , nil
3837}
3938
40- func (db * DB ) StudentByMtknr (ctx context.Context , mtknr string , ntas map [string ]* model.NTA ) (* model.Student , error ) {
41- collectionNames , err := db .studentRegsCollectionNames (ctx )
39+ func (db * DB ) StudentByMtknr (ctx context.Context , mtknr string ) (* model.Student , error ) {
40+ collection := db .Client .Database (db .databaseName ).Collection (collectionStudentRegsPerStudentPlanned )
41+ filter := bson.D {{Key : "mtknr" , Value : mtknr }}
4242
43+ var student model.Student
44+
45+ err := collection .FindOne (ctx , filter ).Decode (& student )
4346 if err != nil {
44- log .Error ().Err (err ).Msg ("cannot get student regs collections " )
47+ log .Error ().Err (err ).Str ( "mtknr" , mtknr ). Msg ("cannot find student by mtknr " )
4548 return nil , err
4649 }
4750
48- var student * model.Student
51+ return & student , nil
52+ }
4953
50- for _ , collectionName := range collectionNames {
51- log .Debug ().Str ("collection" , collectionName ).Str ("mtkntr" , mtknr ).
52- Msg ("searching for student in collection" )
54+ // func (db *DB) StudentByMtknr(ctx context.Context, mtknr string, ntas map[string]*model.NTA) (*model.Student, error) {
55+ // collectionNames, err := db.studentRegsCollectionNames(ctx)
5356
54- collection := db .Client .Database (db .databaseName ).Collection (collectionName )
57+ // if err != nil {
58+ // log.Error().Err(err).Msg("cannot get student regs collections")
59+ // return nil, err
60+ // }
5561
56- cur , err := collection .Find (ctx , bson.D {{Key : "MTKNR" , Value : mtknr }})
57- if err != nil {
58- log .Error ().Err (err ).Str ("collection" , collectionName ).Str ("mtkntr" , mtknr ).
59- Msg ("error while searching for student in collection" )
60- }
61- defer cur .Close (ctx ) //nolint:errcheck
62+ // var student *model.Student
6263
63- var results []* model.StudentReg
64+ // for _, collectionName := range collectionNames {
65+ // log.Debug().Str("collection", collectionName).Str("mtkntr", mtknr).
66+ // Msg("searching for student in collection")
6467
65- err = cur .All (ctx , & results )
66- if err != nil {
67- log .Error ().Err (err ).Str ("collection" , collectionName ).Str ("mtkntr" , mtknr ).
68- Msg ("error while decoding student from collection" )
69- }
68+ // collection := db.Client.Database(db.databaseName).Collection(collectionName)
7069
71- if len (results ) > 0 {
72- log .Debug ().Interface ("regs" , results ).Str ("collection" , collectionName ).Str ("mtkntr" , mtknr ).
73- Msg ("found regs for student" )
74-
75- var regs []int
76-
77- if student != nil && (student .Program != results [0 ].Program ||
78- student .Group != results [0 ].Group ||
79- student .Name != results [0 ].Name ) {
80- log .Error ().Str ("collection" , collectionName ).Str ("mtkntr" , mtknr ).
81- Msg ("found student in more than one programs" )
82- }
83-
84- if student != nil {
85- regs = student .Regs
86- } else {
87- regs = make ([]int , 0 , len (results ))
88- }
89-
90- for _ , res := range results {
91- regs = append (regs , res .AnCode )
92- }
93-
94- sort .Ints (regs )
95-
96- var nta * model.NTA
97-
98- if ntas == nil {
99- nta , err = db .Nta (ctx , mtknr )
100- if err != nil {
101- log .Error ().Err (err ).Str ("mtknr" , mtknr ).Msg ("error while checking nta" )
102- }
103- } else {
104- nta = ntas [mtknr ]
105- }
106-
107- student = & model.Student {
108- Mtknr : mtknr ,
109- Program : results [0 ].Program ,
110- Group : results [0 ].Group ,
111- Name : results [0 ].Name ,
112- Regs : regs ,
113- Nta : nta ,
114- }
70+ // cur, err := collection.Find(ctx, bson.D{{Key: "MTKNR", Value: mtknr}})
71+ // if err != nil {
72+ // log.Error().Err(err).Str("collection", collectionName).Str("mtkntr", mtknr).
73+ // Msg("error while searching for student in collection")
74+ // }
75+ // defer cur.Close(ctx) //nolint:errcheck
11576
116- }
77+ // var results []*model.StudentReg
11778
118- }
79+ // err = cur.All(ctx, &results)
80+ // if err != nil {
81+ // log.Error().Err(err).Str("collection", collectionName).Str("mtkntr", mtknr).
82+ // Msg("error while decoding student from collection")
83+ // }
11984
120- return student , nil
121- }
85+ // if len(results) > 0 {
86+ // log.Debug().Interface("regs", results).Str("collection", collectionName).Str("mtkntr", mtknr).
87+ // Msg("found regs for student")
88+
89+ // var regs []int
90+
91+ // if student != nil && (student.Program != results[0].Program ||
92+ // student.Group != results[0].Group ||
93+ // student.Name != results[0].Name) {
94+ // log.Error().Str("collection", collectionName).Str("mtkntr", mtknr).
95+ // Msg("found student in more than one programs")
96+ // }
97+
98+ // if student != nil {
99+ // regs = student.Regs
100+ // } else {
101+ // regs = make([]int, 0, len(results))
102+ // }
103+
104+ // for _, res := range results {
105+ // regs = append(regs, res.AnCode)
106+ // }
107+
108+ // sort.Ints(regs)
109+
110+ // var nta *model.NTA
111+
112+ // if ntas == nil {
113+ // nta, err = db.Nta(ctx, mtknr)
114+ // if err != nil {
115+ // log.Error().Err(err).Str("mtknr", mtknr).Msg("error while checking nta")
116+ // }
117+ // } else {
118+ // nta = ntas[mtknr]
119+ // }
120+
121+ // student = &model.Student{
122+ // Mtknr: mtknr,
123+ // Program: results[0].Program,
124+ // Group: results[0].Group,
125+ // Name: results[0].Name,
126+ // Regs: regs,
127+ // Nta: nta,
128+ // }
129+
130+ // }
131+
132+ // }
133+
134+ // return student, nil
135+ // }
122136
123137func (db * DB ) StudentsByName (ctx context.Context , regex string ) ([]* model.Student , error ) {
124138 collectionNames , err := db .studentRegsCollectionNames (ctx )
@@ -152,7 +166,7 @@ func (db *DB) StudentsByName(ctx context.Context, regex string) ([]*model.Studen
152166 students := make ([]* model.Student , 0 , studentMtknrs .Cardinality ())
153167
154168 for _ , mtknr := range studentMtknrs .ToSlice () {
155- student , err := db .StudentByMtknr (ctx , mtknr , nil )
169+ student , err := db .StudentByMtknr (ctx , mtknr )
156170 if err != nil {
157171 log .Error ().Err (err ).Str ("mtknr" , mtknr ).Msg ("error while trying to get student" )
158172 } else {
0 commit comments