public HttpResponseMessage CreateStudent([FromBody] StudentDTO newStudent)
 {
     using (var context = new CEISSContext())
     {
         try
         {
             context.Students.Add(new Student
             {
                 BirthDate      = newStudent.BirthDate,
                 Email          = newStudent.Email,
                 CreationDate   = DateTime.UtcNow,
                 CareerID       = newStudent.CareerID,
                 Cellphone      = newStudent.Cellphone,
                 FirstName      = newStudent.FirstName,
                 SecondName     = newStudent.SecondName,
                 FirstLastName  = newStudent.FirstLastName,
                 SecondLastName = newStudent.SecondLastName,
                 Phone          = newStudent.Phone,
             });
             context.SaveChanges();
             return(Request.CreateResponse(HttpStatusCode.OK));
         }
         catch (Exception)
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
     }
 }
        public HttpResponseMessage getStudentsByFirstName(string name)
        {
            using (var context = new CEISSContext())
            {
                var Students =
                    context.Students
                    .Where(student => student.FirstName == name)
                    .ToList();

                /*
                 *
                 * Other ways to query the database:
                 *
                 * var Students_ = (from s in context.Students
                 *                where s.FirstName == name
                 *                select s).ToList();
                 *
                 * var Students_ = (from s in context.Students
                 *                where s.FirstName == name
                 *                select s).ToList();
                 *
                 * var _STUDENTS = context.Database.SqlQuery<Student>("SELECT * FROM USERS").ToList();
                 *
                 */

                return(Request.CreateResponse(HttpStatusCode.OK, Students));
            }
        }
        public HttpResponseMessage DeleteStudent([FromUri] int studentID)
        {
            using (var context = new CEISSContext())
            {
                try
                {
                    var StudentInDb = context.Students.Where(U => U.StudentID == studentID).FirstOrDefault();
                    if (StudentInDb != null)
                    {
                        context.Students.Remove(StudentInDb);
                        context.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound));
                    }
                }
                catch (Exception)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }
            }
        }
        public HttpResponseMessage UpdateStudent([FromUri] int studentID, [FromBody] StudentDTO StudentChanges)
        {
            using (var context = new CEISSContext())
            {
                try
                {
                    var StudentInDb = context.Students.Where(U => U.StudentID == studentID).FirstOrDefault();
                    if (StudentInDb != null)
                    {
                        StudentInDb.BirthDate = StudentChanges.BirthDate;
                        context.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound));
                    }
                }
                catch (Exception)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }
            }
        }
Exemplo n.º 5
0
        public object GetCarreers()
        {
            using (var context = new CEISSContext())
            {
                var results = context.Careers.Select(d => new
                {
                    carreerID = d.CareerId,
                    code      = d.CareerCode,
                    name      = d.CareerName
                }).ToList();

                if (results != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, results));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
        }
Exemplo n.º 6
0
 public object GetArticles(int page)
 {
     using (var context = new CEISSContext(false))
     {
         int postPerPage = 6;
         page = page - 1;
         var Articles = context.Articles
                        .OrderByDescending(o => o.PublishDate)
                        .Skip(postPerPage * page)
                        .Take(postPerPage)
                        .Select(d => new
         {
             d.Title,
             d.Preview,
             d.PrimaryImage,
             author = new
             {
                 name     = d.Author.FirstName,
                 lastname = d.Author.FirstLastName,
             }
         }).FirstOrDefault();
         return(Articles);
     }
 }