Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public HttpResponseMessage Post(User user)
        {
            HttpResponseMessage msg;

            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                try
                {
                    var u = _entites.Users.FirstOrDefault(c => c.Username == user.Username);
                    if (u == null)
                    {
                        _entites.Users.Add(user);
                        _entites.SaveChanges();

                        msg = Request.CreateResponse(HttpStatusCode.Created, user);
                        msg.Headers.Location = new Uri(Request.RequestUri + "/" + user.ID);
                    }
                    else
                    {
                        msg = Request.CreateErrorResponse(HttpStatusCode.Conflict, "Error! This UserName : "******" Is Not Available.");
                    }


                    return(msg);
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
        }
Ejemplo n.º 2
0
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <param name="doctor"></param>
/// <returns></returns>

        public HttpResponseMessage Put(int ID, Doctor doctor)
        {
            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                try
                {
                    var myDoctor = _entites.Doctors.FirstOrDefault(d => d.Id == ID);
                    if (myDoctor == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User is not Found with this ID : " + ID));
                    }
                    else
                    {
                        myDoctor.Name  = doctor.Name;
                        myDoctor.Title = doctor.Title;

                        if (doctor.Image != null)
                        {
                            myDoctor.Image = doctor.Image;
                        }

                        _entites.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, doctor));
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        public HttpResponseMessage Post(Course course)
        {
            HttpResponseMessage msg;

            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                try
                {   //prevent Add course with an exist Cod
                    var crs = _entites.Courses.FirstOrDefault(c => c.Code == course.Code);
                    if (crs == null)
                    {
                        _entites.Courses.Add(course);
                        _entites.SaveChanges();


                        msg = Request.CreateResponse(HttpStatusCode.Created, course);
                        msg.Headers.Location = new Uri(Request.RequestUri + "/" + course.ID);
                    }
                    else
                    {
                        msg = Request.CreateErrorResponse(HttpStatusCode.Conflict, "Error! This Code : " + course.Code + " Is Not Available.");
                    }

                    return(msg);
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>

        public HttpResponseMessage Delete(int ID)
        {
            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                var user = _entites.Users.FirstOrDefault(c => c.ID == ID);
                if (user == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User is not Found with this ID : " + ID));
                }
                else
                {
                    _entites.Users.Remove(user);
                    _entites.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
        }
Ejemplo n.º 5
0
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <param name="myuser"></param>
/// <returns></returns>

        public HttpResponseMessage Put(int ID, User myuser)
        {
            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                try
                {
                    var u = _entites.Users.FirstOrDefault(c => c.Username == myuser.Username && c.ID != myuser.ID);

                    if (u == null)
                    {
                        var user = _entites.Users.FirstOrDefault(c => c.ID == ID);
                        if (user == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User is not Found with this ID : " + ID));
                        }
                        else
                        {
                            user.FirstName = myuser.FirstName;
                            user.LastName  = myuser.LastName;
                            user.Email     = myuser.Email;
                            user.Gender    = myuser.Gender;
                            if (myuser.Image != null)
                            {
                                user.Image = myuser.Image;
                            }

                            user.Username = myuser.Username;
                            user.Password = myuser.Password;

                            _entites.SaveChanges();
                            return(Request.CreateResponse(HttpStatusCode.OK, myuser));
                        }
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.Conflict, "Error! This UserName : "******" Is Not Available."));
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="doctor"></param>
        /// <returns></returns>
        public HttpResponseMessage Post(Doctor doctor)
        {
            HttpResponseMessage msg;

            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                try
                {
                    _entites.Doctors.Add(doctor);
                    _entites.SaveChanges();

                    msg = Request.CreateResponse(HttpStatusCode.Created, doctor);
                    msg.Headers.Location = new Uri(Request.RequestUri + "/" + doctor.Id);
                    return(msg);
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
        }
Ejemplo n.º 7
0
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
/// <param name="course"></param>
/// <returns></returns>

        public HttpResponseMessage Put(int ID, Course course)
        {
            using (ProjectAPIEntities _entites = new ProjectAPIEntities())
            {
                try
                {
                    //prevent update course with an exist Code
                    var crs = _entites.Courses.FirstOrDefault(c => c.Code == course.Code && c.ID != course.ID);

                    if (crs == null)
                    {
                        var mycourse = _entites.Courses.FirstOrDefault(c => c.ID == ID);
                        if (mycourse == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                               "Course is not Found with this ID : " + ID));
                        }
                        else
                        {
                            mycourse.Code        = course.Code;
                            mycourse.Describtion = course.Describtion;

                            _entites.SaveChanges();

                            return(Request.CreateResponse(HttpStatusCode.OK, course));
                        }
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.Conflict, "Error! This Code : " + course.Code + " Is Not Available."));
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
        }