diff --git a/core/core.exe b/core/core.exe index eae51e7c..0346a10e 100644 Binary files a/core/core.exe and b/core/core.exe differ diff --git a/handlers/organization_handlers.go b/handlers/organization_handlers.go index 477cdff5..fe663e64 100644 --- a/handlers/organization_handlers.go +++ b/handlers/organization_handlers.go @@ -6,7 +6,6 @@ import ( "encoding/json" "Xtern-Matching/handlers/services" "github.com/dgrijalva/jwt-go" - "log" "github.com/gorilla/context" "google.golang.org/appengine/datastore" "Xtern-Matching/models" @@ -17,7 +16,6 @@ func GetOrganizations(w http.ResponseWriter,r *http.Request) { organizations, keys, err := services.GetOrganizations(ctx) if err != nil { - //log.Print(err) http.Error(w, err.Error(), 500) return } @@ -61,27 +59,32 @@ func GetOrganizationStudents(w http.ResponseWriter,r *http.Request) { return } - org, err := services.GetOrganization(ctx,orgKey) if err != nil { http.Error(w, err.Error(), 500) return } - students := make([]models.Student,0) - for _, key := range org.Students { - student, err := services.GetStudent(ctx, key) + students := make([]models.Student,len(org.StudentRanks)) + keys :=make([]*datastore.Key,len(org.StudentRanks)) + for _, studentRank := range org.StudentRanks { + student, err := services.GetStudent(ctx, studentRank.Student) if err != nil { http.Error(w, err.Error(), 500) return } - students = append(students, student) + // students = append(students, student) + // keys = append(keys, studentRank.Student) + students[studentRank.Rank] = student + keys[studentRank.Rank] = studentRank.Student } + + type Response struct { Keys []*datastore.Key `json:"keys"` Students []models.Student `json:"students"` } - response := Response{Keys: org.Students, Students: students} + response := Response{Keys: keys, Students: students} w.Header().Add("Access-Control-Allow-Origin", "*") @@ -152,40 +155,6 @@ func RemoveStudentFromOrganization(w http.ResponseWriter,r *http.Request) { w.WriteHeader(http.StatusOK) } -// func MoveStudentInOrganization(w http.ResponseWriter,r *http.Request) { -// ctx := appengine.NewContext(r) - -// var dat map[string]interface{} -// decoder := json.NewDecoder(r.Body) -// if err := decoder.Decode(&dat); err != nil { -// http.Error(w, err.Error(), 500) -// return -// } -// studentKey, err := datastore.DecodeKey(dat["studentKey"].(string)) -// if err != nil { -// http.Error(w, err.Error(), 500) -// return -// } - -// user := context.Get(r, "user") -// mapClaims := user.(*jwt.Token).Claims.(jwt.MapClaims) -// orgKey, err := datastore.DecodeKey(mapClaims["org"].(string)) -// if err != nil { -// log.Println(err.Error()) -// http.Error(w, err.Error(), 500) -// return -// } -// position := int(dat["position"].(float64)); - -// _, err = services.MoveStudentInOrganization(ctx, orgKey, studentKey, position) -// if err != nil { -// log.Print(err) -// http.Error(w, err.Error(), 500) -// return -// } -// w.WriteHeader(http.StatusOK) -// } - func SwitchStudentsInOrganization(w http.ResponseWriter,r *http.Request) { ctx := appengine.NewContext(r) @@ -210,14 +179,11 @@ func SwitchStudentsInOrganization(w http.ResponseWriter,r *http.Request) { mapClaims := user.(*jwt.Token).Claims.(jwt.MapClaims) orgKey, err := datastore.DecodeKey(mapClaims["org"].(string)) if err != nil { - log.Println(err.Error()) http.Error(w, err.Error(), 500) return } - _, err = services.SwitchStudentsInOrganization(ctx, orgKey, studentKey1, studentKey2) if err != nil { - log.Print(err) http.Error(w, err.Error(), 500) return } @@ -231,7 +197,6 @@ func GetCurrentOrganization(w http.ResponseWriter,r *http.Request) { mapClaims := user.(*jwt.Token).Claims.(jwt.MapClaims) orgKey, err := datastore.DecodeKey(mapClaims["org"].(string)) if err != nil { - log.Println(err.Error()) http.Error(w, err.Error(), 500) return } diff --git a/handlers/services/organization_services.go b/handlers/services/organization_services.go index 96819921..94b8fd1e 100644 --- a/handlers/services/organization_services.go +++ b/handlers/services/organization_services.go @@ -66,33 +66,13 @@ func RemoveStudentFromOrganization(ctx context.Context, orgKey *datastore.Key, s return nil } -// func MoveStudentInOrganization(ctx context.Context, orgKey int64, studentKey *datastore.Key, pos int) (int64,error) { -// orgKey := datastore.NewKey(ctx, "Company", "", companyId, nil) -// var org models.Organization -// if err := datastore.Get(ctx, orgKey, &org); err != nil { -// return http.StatusInternalServerError, err -// } - -// org.MoveStudent(studentKey, pos) - -// if _, err := datastore.Put(ctx, orgKey, &org); err != nil { -// return http.StatusInternalServerError, err -// } -// return orgKey.IntID(), nil -// } - func SwitchStudentsInOrganization(ctx context.Context, orgKey *datastore.Key, student1Id *datastore.Key, student2Id *datastore.Key) (int64,error) { var company models.Organization if err := datastore.Get(ctx, orgKey, &company); err != nil { return http.StatusInternalServerError, err } - // company.Id = companyId - if contains(company.Students, student1Id) && contains(company.Students, student2Id) { - company.Students = switchElements(company.Students, student1Id, student2Id); - } else { - return http.StatusInternalServerError, nil - } + company.StudentRanks = switchElements(company.StudentRanks, student1Id, student2Id); if _, err := datastore.Put(ctx, orgKey, &company); err != nil { return http.StatusInternalServerError, err @@ -100,24 +80,15 @@ func SwitchStudentsInOrganization(ctx context.Context, orgKey *datastore.Key, st return orgKey.IntID(), nil } -//func GetOrganization(ctx context.Context, orgKey datastore.Key) (models.Organization,error) { -// //orgKey := datastore.NewKey(ctx, "Organization", "", _id, nil) -// var org models.Organization -// if err := datastore.Get(ctx, orgKey, &org); err != nil { -// return models.Organization{}, err -// } -// return org, nil -//} - -func switchElements(array []*datastore.Key, a *datastore.Key, b *datastore.Key) []*datastore.Key { - for i := 0; i < len(array); i++ { - if array[i] == a { - array[i] = b - } else if array[i] == b { - array[i] = a - } - } - return array +func switchElements(ranks []models.StudentRank, a *datastore.Key, b *datastore.Key) []models.StudentRank { + for i, studentRank := range ranks { + if studentRank.Student.Equal(a) { + ranks[i].Student = b + } else if studentRank.Student.Equal(b) { + ranks[i].Student = a + } + } + return ranks; } func contains(array []*datastore.Key, element *datastore.Key) bool { diff --git a/models/organization.go b/models/organization.go index d352a1c7..d587630d 100644 --- a/models/organization.go +++ b/models/organization.go @@ -7,27 +7,35 @@ import ( type Organization struct { Name string `json:"name"` - Students []*datastore.Key `json:"students"` + StudentRanks []StudentRank `json:"students"` +} + +type StudentRank struct { + Student *datastore.Key `json:"student"` + Rank int `json:"rank"` } func NewOrganization(name string) Organization { - students := make([]*datastore.Key,0) - return Organization{Name: name, Students: students} + studentRanks := make([]StudentRank,0) + return Organization{Name: name, StudentRanks: studentRanks} } func (org *Organization) AddStudent(studentKey *datastore.Key) bool { - for _, key := range org.Students { - if key.Equal(studentKey) { + for _, studentRank := range org.StudentRanks { + if studentRank.Student.Equal(studentKey) { return false } } - org.Students = append(org.Students,studentKey) + var newStudentRank StudentRank + newStudentRank.Student = studentKey + newStudentRank.Rank = len(org.StudentRanks) + org.StudentRanks = append(org.StudentRanks,newStudentRank) return true } func (org *Organization) ContainStudent(studentKey *datastore.Key) int { - for i, key := range org.Students { - if key.Equal(studentKey) { + for i, studentRank := range org.StudentRanks { + if studentRank.Student.Equal(studentKey) { return i } } @@ -35,32 +43,32 @@ func (org *Organization) ContainStudent(studentKey *datastore.Key) int { } func (org *Organization) RemoveStudent(studentKey *datastore.Key) { - for i, key := range org.Students { - if key.Equal(studentKey) { - org.Students = append(org.Students[:i], org.Students[i+1:]...) + for i, studentRank := range org.StudentRanks { + if studentRank.Student.Equal(studentKey) { + org.StudentRanks = append(org.StudentRanks[:i], org.StudentRanks[i+1:]...) return } } } func (org *Organization) MoveStudent(studentKey *datastore.Key, pos int) { - students := make([]*datastore.Key,len(org.Students)) + studentRanks := make([]StudentRank,len(org.StudentRanks)) i := 0 j := 0 - for i < len(org.Students) && j < len(students) { + for i < len(org.StudentRanks) && j < len(studentRanks) { if pos == j { log.Printf("Posisiton=%v", i) - students[j] = studentKey + studentRanks[j].Student = studentKey j++ - } else if org.Students[i].Equal(studentKey) { + } else if org.StudentRanks[i].Student.Equal(studentKey) { log.Printf("Remove=%v", i) i++ } else { - students[j] = org.Students[i] + studentRanks[j] = org.StudentRanks[i] i++ j++ } } - org.Students = students + org.StudentRanks = studentRanks }