Exemplo n.º 1
0
        public HttpResponseMessage Put(int id, [FromBody] Student student)
        {
            try
            {
                using (StudentsDBEntities db = new StudentsDBEntities())
                {
                    var _student = db.Students.FirstOrDefault(s => s.Id == id);

                    if (_student == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with Id " + id.ToString() + " not found to edit."));
                    }
                    else
                    {
                        _student.FirstName = student.FirstName;
                        _student.LastName  = student.LastName;
                        _student.Birthday  = student.Birthday;

                        db.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, _student));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage PUT([FromBody] int id, Student student)
        {
            try {
                using (StudentsDBEntities entities = new StudentsDBEntities())
                {
                    var entity = entities.Students.FirstOrDefault(e => e.ID == id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with Id = " + id.ToString() + " not found to update"));
                    }
                    else
                    {
                        entity.FirstName    = student.FirstName;
                        entity.LastName     = student.LastName;
                        entity.Gender       = student.Gender;
                        entity.YearAdmitted = student.YearAdmitted;
                        entity.Class        = student.Class;

                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemplo n.º 3
0
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (StudentsDBEntities db = new StudentsDBEntities())
                {
                    var student = db.Students.FirstOrDefault(s => s.Id == id);

                    if (student == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with Id " + id + " not found to delete."));
                    }
                    else
                    {
                        db.Students.Remove(student);
                        db.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
 public ActionResult Create([Bind(Exclude = "Id")] Student studentToCreate)
 {
     if (!ModelState.IsValid)
     {
         return(View());
     }
     _db.Students.Add(studentToCreate);
     _db.SaveChanges();
     return(RedirectToAction("Details/" + studentToCreate.Id));
 }
Exemplo n.º 5
0
        private void EdytujStudentaBtn_Click(object sender, RoutedEventArgs e)
        {
            StudentsDBEntities SDE = new StudentsDBEntities();
            var ST = SDE.StudentsTable.Where(p => p.Id == row.Id).FirstOrDefault();

            ST.StudentName    = ImieTb.Text;
            ST.StudentSurname = NazwiskoTb.Text;
            ST.DateOfBirt     = DataDp.SelectedDate.Value;
            SDE.SaveChanges();
            DialogResult = true;
        }
Exemplo n.º 6
0
 public IHttpActionResult Delete(int id)
 {
     using (StudentsDBEntities ctx = new StudentsDBEntities())
     {
         var studentToDelete  = ctx.Students.FirstOrDefault(s => s.ID == id);
         var gradesAndCourses = from studentCourses in ctx.StudentCourses where studentCourses.StudentID == id select studentCourses;
         foreach (StudentCourses element in gradesAndCourses)
         {
             ctx.StudentCourses.Remove(element);
         }
         ctx.Students.Remove(studentToDelete);
         return(Ok(ctx.SaveChanges()));
     }
 }
Exemplo n.º 7
0
        public IHttpActionResult Post([FromBody] Students student)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (StudentsDBEntities ctx = new StudentsDBEntities())
            {
                var maxId = ctx.Students.ToList().Select(s => s).Max(x => x.ID);
                student.ID = maxId + 1;

                ctx.Students.Add(student);
                return(Ok(ctx.SaveChanges()));
            }
        }
Exemplo n.º 8
0
        public IHttpActionResult Put([FromBody] Courses course, int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (StudentsDBEntities ctx = new StudentsDBEntities())
            {
                var courseToEdit = ctx.Courses.FirstOrDefault(c => c.ID == id);

                courseToEdit.Name      = course.Name;
                courseToEdit.DayLength = course.DayLength;
                return(Ok(ctx.SaveChanges()));
            }
        }
Exemplo n.º 9
0
        private void DodajOceneBtn_Click(object sender, RoutedEventArgs e)
        {
            StudentsDBEntities SDE = new StudentsDBEntities();
            AssessmentsTable   AT  = new AssessmentsTable();
            var checkedAssessment  = OcenySp.Children.OfType <RadioButton>()
                                     .FirstOrDefault(r => r.IsChecked.HasValue && r.IsChecked.Value);
            var checkedSubject = PrzedmiotySp.Children.OfType <RadioButton>()
                                 .FirstOrDefault(r => r.IsChecked.HasValue && r.IsChecked.Value);

            AT.Assessment = Decimal.Parse(checkedAssessment.Content.ToString().Replace(".", ","));
            AT.IndeksID   = indeksId;
            AT.Subjects   = checkedSubject.Content.ToString();
            SDE.AssessmentsTable.Add(AT);
            SDE.SaveChanges();
            DialogResult = true;
        }
Exemplo n.º 10
0
        public IHttpActionResult Put([FromBody] Grades grade, int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (StudentsDBEntities ctx = new StudentsDBEntities())
            {
                var gradeToEdit = ctx.Grades.FirstOrDefault(g => g.ID == id);

                gradeToEdit.LetterEquivalent = grade.LetterEquivalent;
                gradeToEdit.NumberEquivalent = grade.NumberEquivalent;
                return(Ok(ctx.SaveChanges()));
            }
        }
Exemplo n.º 11
0
        public IHttpActionResult Put([FromBody] Students student, int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (StudentsDBEntities ctx = new StudentsDBEntities())
            {
                var studentToEdit = ctx.Students.FirstOrDefault(s => s.ID == id);

                studentToEdit.Name        = student.Name;
                studentToEdit.Surname     = student.Surname;
                studentToEdit.City        = student.City;
                studentToEdit.DateOfBirth = student.DateOfBirth;
                return(Ok(ctx.SaveChanges()));
            }
        }
Exemplo n.º 12
0
        public HttpResponseMessage Post([FromBody] Student student)
        {
            try {
                using (StudentsDBEntities entities = new StudentsDBEntities())
                {
                    entities.Students.Add(student);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, student);
                    message.Headers.Location = new Uri(Request.RequestUri + student.ID.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemplo n.º 13
0
        private void DodajStudentaBtn_Click(object sender, RoutedEventArgs e)
        {
            int id = Int32.Parse(IndeksTb.Text);
            StudentsDBEntities SDE = new StudentsDBEntities();
            StudentsTable      ST  = new StudentsTable();
            var czy_istnieje       = SDE.StudentsTable.Where(p => p.Id == id).FirstOrDefault();

            if (czy_istnieje == null)
            {
                ST.Id             = Int32.Parse(IndeksTb.Text);
                ST.StudentName    = ImieTb.Text;
                ST.StudentSurname = NazwiskoTb.Text;
                ST.DateOfBirt     = DataDp.SelectedDate.Value;
                SDE.StudentsTable.Add(ST);
                SDE.SaveChanges();
            }
            else
            {
                MessageBox.Show("Istnieje już taki student");
            }
            DialogResult = true;
        }
Exemplo n.º 14
0
        public HttpResponseMessage Delete(int id)
        {
            try {
                using (StudentsDBEntities entities = new StudentsDBEntities())
                {
                    var entity = entities.Students.FirstOrDefault(e => e.ID == id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with Id = " + id.ToString() + " not found to delete"));
                    }
                    else
                    {
                        entities.Students.Remove(entity);
                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }