public Grades createGrade(int realisationId, string name, string maxValue)
 {
     using (var db = new NTR2013Entities())
     {
         var grade = new Grades { RealisationID = realisationId, Name = name, MaxValue = maxValue };
         db.Grades.Add(grade);
         db.SaveChanges();
         return grade;
     }
 }
        //dodaje ocene do realizacji
        //szuka realizacji
        //sprawdza czy name jest unikatowe w realizacji
        public GradesResult addGrades(int realizationId, string name, string maxValue)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Realisations.Find(realizationId);
                /*
                List<Realisations> i = db.Realisations.ToList();
                bool f = false;
                foreach (Realisations r in i)
                {
                    if (r.RealisationID == realizationId)
                    {
                        f = true;
                        break;
                    }
                }
                */
                //
                if (original == null)
                    return new GradesResult(ErrorMessage.ADD_GRADES_COULD_NOT_FIND_REALIZATIONS_ID);

                List<Grades> list = getGrades(realizationId);
                foreach (Grades g in list)
                {
                    if (g.Name.Equals(name))
                        return new GradesResult(ErrorMessage.ADD_GRADES_NAME_EXISTS);
                }

                Grades grade = new Grades { RealisationID = realizationId, Name = name, MaxValue = maxValue, Realisations = null };
                Grades result = db.Grades.Add(grade);
                db.SaveChanges();
                result.Realisations = null;

                return new GradesResult(result);
            }
        }
 public GradesResult(string msg)
 {
     this.data = null;
     this.msg = msg;
 }
 public GradesResult(Grades data)
 {
     this.data = data;
     this.msg = null;
 }
        public GradesResult removeGrades(int id, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Grades.Find(id);
                if (original != null)
                {
                    //Console.WriteLine("sfasdfasdf stamp" + Convert.ToString(original.TimeStamp));
                    if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                    {
                        GradesResult result = new GradesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                        Grades grade = new Grades { GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null };
                        result.data = grade;
                        return result;
                    }

                    List<GradeValues> list = db.GradeValues.ToList();
                    string studentsToEdit = "";

                    foreach (GradeValues g in list)
                    {
                        if (g.GradeID == id)
                        {
                            //Value 10 pleaces
                            if (g.Value.Equals("          "))
                                db.GradeValues.Remove(g);
                            else
                            {
                                //sprawdzenie studentow z ocena
                                var originalRegistration = db.Registrations.Find(g.RegistrationID);
                                var originalStudent = db.Students.Find(originalRegistration.StudentID);
                                studentsToEdit += originalStudent.FirstName + " " + originalStudent.LastName + ", ";
                            }
                        }
                    }

                    if (!studentsToEdit.Equals(""))
                    {
                        studentsToEdit = studentsToEdit.Substring(0, studentsToEdit.Length - 2);
                        return new GradesResult(studentsToEdit);
                    }

                    db.Grades.Remove(original);
                    db.SaveChanges();
                    return new GradesResult(original);
                }

                return new GradesResult(ErrorMessage.REMOVE_GRADES_COULD_NOT_FIND_GRADES_ID);
            }
        }
        public GradesResult editGrades(int gradeId, string name, string maxValue, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Grades.Find(gradeId);
                if (original != null)
                {
                    if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                    {
                        GradesResult result = new GradesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                        Grades g = new Grades { GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null };
                        result.data = g;
                        return result;
                    }

                    List<Grades> list = getGrades(original.RealisationID);
                    foreach (Grades g in list)
                    {
                        if (g.Name.Equals(name))
                            return new GradesResult(ErrorMessage.EDIT_GRADES_NAME_EXISTS);
                    }

                    if (!original.MaxValue.Equals(maxValue))
                    {
                        List<GradeValues> l = db.GradeValues.ToList();
                        string studentsToEdit = "";

                        foreach (GradeValues g in l)
                        {
                            if (g.GradeID == original.GradeID)
                            {
                                //Value 10 pleaces
                                if (!g.Value.Equals("          "))
                                {
                                    //sprawdzenie studentow z ocena
                                    var originalRegistration = db.Registrations.Find(g.RegistrationID);
                                    var originalStudent = db.Students.Find(originalRegistration.StudentID);
                                    studentsToEdit += originalStudent.FirstName + " " + originalStudent.LastName + ", ";
                                }
                            }
                        }

                        if (!studentsToEdit.Equals(""))
                        {
                            studentsToEdit = studentsToEdit.Substring(0, studentsToEdit.Length - 2);
                            return new GradesResult(studentsToEdit);
                        }
                    }

                    original.Name = name;
                    original.MaxValue = maxValue;
                    //original.TimeStamp = null;
                    db.SaveChanges();

                    Grades grade = new Grades { GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null };

                    return new GradesResult(grade);
                }

                return new GradesResult(ErrorMessage.EDIT_GRADES_COULD_NOT_FIND_GRADES_ID);
            }
        }