public HttpResponseMessage Get([FromUri] preg_contact_us data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         IEnumerable <preg_contact_us> result;
         if (!data.DeepEquals(new preg_contact_us()))
         {
             data.user_id = user_id;
             result       = dao.GetItemsByParams(data);
         }
         else
         {
             result = dao.GetListItem().Where(c => c.user_id == user_id);
         }
         if (result.Count() > 0)
         {
             return(Request.CreateResponse(HttpStatusCode.OK, result));
         }
         else
         {
             HttpError err = new HttpError(SysConst.DATA_NOT_FOUND);
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, err));
         }
     }
     catch (Exception ex)
     {
         HttpError err = new HttpError(ex.Message);
         return(Request.CreateErrorResponse(HttpStatusCode.NotFound, err));
     }
 }
Beispiel #2
0
        public IQueryable <preg_contact_us> GetItemsByParams(preg_contact_us data)
        {
            IQueryable <preg_contact_us> result = connect.preg_contact_us;

            for (int i = 0; i < data.GetType().GetProperties().ToList().Count(); i++)
            {
                string propertyName  = data.GetType().GetProperties().ToList()[i].Name;
                var    propertyValue = data.GetType().GetProperty(propertyName).GetValue(data, null);
                if (propertyName == "id" && (int)(propertyValue) != 0)
                {
                    result = result.Where(c => c.id == (int)(propertyValue));
                }
                else if (propertyName == "user_id" && propertyValue != null)
                {
                    result = result.Where(c => c.user_id == (int)(propertyValue));
                }
                else if (propertyName == "email" && propertyValue != null)
                {
                    result = result.Where(c => SqlFunctions.PatIndex("%" + propertyValue.ToString() + "%", c.email) > 0);
                }
                else if (propertyName == "message" && propertyValue != null)
                {
                    result = result.Where(c => SqlFunctions.PatIndex("%" + propertyValue.ToString() + "%", c.message) > 0);
                }
            }
            return(result);
        }
        public HttpResponseMessage Put([FromBody] preg_contact_us dataUpdate)
        {
            try
            {
                int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
                if (!dataUpdate.DeepEquals(new preg_contact_us()))
                {
                    preg_contact_us contact_us = new preg_contact_us();
                    contact_us = dao.GetItemsByParams(new preg_contact_us()
                    {
                        user_id = user_id
                    }).FirstOrDefault();
                    if (contact_us == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
                    }
                    if (dataUpdate.email != null)
                    {
                        contact_us.email = dataUpdate.email;
                    }
                    if (dataUpdate.message != null)
                    {
                        contact_us.message = dataUpdate.message;
                    }

                    dao.UpdateData(contact_us);
                    return(Request.CreateResponse(HttpStatusCode.Accepted, SysConst.DATA_UPDATE_SUCCESS));
                }
                else
                {
                    HttpError err = new HttpError(SysConst.DATA_NOT_EMPTY);
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
                }
            }
            catch (Exception ex)
            {
                HttpError err = new HttpError(ex.Message);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
            }
        }
 public HttpResponseMessage Delete()
 {
     try
     {
         int             user_id    = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         preg_contact_us contact_us = dao.GetItemsByParams(new preg_contact_us()
         {
             user_id = user_id
         }).FirstOrDefault();
         if (contact_us == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
         }
         dao.DeleteData(contact_us);
         return(Request.CreateResponse(HttpStatusCode.Accepted, SysConst.DATA_DELETE_SUCCESS));
     }
     catch (Exception ex)
     {
         HttpError err = new HttpError(ex.Message);
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
     }
 }
 public HttpResponseMessage Post([FromBody] preg_contact_us data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         if (!data.DeepEquals(new preg_contact_us()))
         {
             data.user_id = user_id;
             dao.InsertData(data);
             return(Request.CreateResponse(HttpStatusCode.Created, SysConst.DATA_INSERT_SUCCESS));
         }
         else
         {
             HttpError err = new HttpError(SysConst.DATA_NOT_EMPTY);
             return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
         }
     }
     catch (Exception ex)
     {
         HttpError err = new HttpError(ex.Message);
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
     }
 }
Beispiel #6
0
 public void DeleteData(preg_contact_us item)
 {
     connect.preg_contact_us.Remove(item);
     connect.SaveChanges();
 }
Beispiel #7
0
 public void UpdateData(preg_contact_us item)
 {
     connect.SaveChanges();
 }
Beispiel #8
0
 public void InsertData(preg_contact_us item)
 {
     connect.preg_contact_us.Add(item);
     connect.SaveChanges();
 }