Esempio n. 1
0
        //public HttpResponseMessage PostAng_Employees([FromBody]ANG_EMPLOYEE employee) {
        //    try
        //    {
        //        using (OVODEntities entities = new OVODEntities()) {
        //            entities.ANG_EMPLOYEE.Add(employee);
        //            entities.SaveChanges();
        //            var message = Request.CreateResponse(HttpStatusCode.Created, employee);
        //            message.Headers.Location = new Uri(Request.RequestUri + employee.id.ToString());
        //            return message;
        //        }
        //    }
        //    catch (Exception ex){
        //        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        //    }
        //}

        public HttpResponseMessage PutAng_Employees([FromBody] ANG_EMPLOYEE employee)
        {
            try
            {
                string imageName   = null;
                var    httpRequest = System.Web.HttpContext.Current.Request;
                var    postedFile  = httpRequest.Files["Image"];
                imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
                imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
                var filePath = HttpContext.Current.Server.MapPath("~/Image/" + imageName);
                postedFile.SaveAs(imageName);

                using (OVODEntities entities = new OVODEntities())
                {
                    var entity = entities.ANG_EMPLOYEE.FirstOrDefault(e => e.id == employee.id);
                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee id: " + employee.id.ToString() + " not found to update."));
                    }
                    else
                    {
                        entity.name        = employee.name;
                        entity.isActive    = employee.isActive;
                        entity.password    = employee.password;
                        entity.PhoneNumber = employee.PhoneNumber;
                        //entity.PhotoPath = employee.PhotoPath;
                        entity.PhotoPath         = imageName;
                        entity.gender            = employee.gender;
                        entity.email             = employee.email;
                        entity.department        = employee.department;
                        entity.dateofBirth       = employee.dateofBirth;
                        entity.ContactPreference = employee.ContactPreference;
                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK, entity));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Esempio n. 2
0
        public HttpResponseMessage Post([FromBody] ANG_EMPLOYEE eMPLOYEE)
        {
            try
            {
                //string imageName = null;
                //var httpRequest = System.Web.HttpContext.Current.Request;
                //var postedFile = httpRequest.Files["Image"];
                //imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "_");
                //imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
                //postedFile.SaveAs(imageName);

                //if (eMPLOYEE.Image != null)
                //{


                //    //eMPLOYEE.Image.SaveAs(Path.Combine(filePath));

                //}
                using (OVODEntities entities = new OVODEntities())
                {
                    //eMPLOYEE.PhotoPath = imageName;
                    entities.ANG_EMPLOYEE.Add(eMPLOYEE);
                    entities.SaveChanges();
                }
                //if (eMPLOYEE.Image != null)
                //{
                //    imageName = Path.GetFileNameWithoutExtension(eMPLOYEE.Image.FileName);
                //    string imageExtn = Path.GetExtension(eMPLOYEE.Image.FileName);
                //    imageName = imageName + DateTime.Now.ToString("yymmssfff") + imageExtn;
                //    var filePath = HttpContext.Current.Server.MapPath("~/Image/" + imageName);
                //    eMPLOYEE.Image.SaveAs(Path.Combine(filePath));
                //}
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }