public HttpResponseMessage Post([FromBody] preg_baby_name data)
        {
            try
            {
                int user_id = Convert.ToInt32(((ClaimsIdentity)User.Identity).FindFirst("id").Value);
                if (!data.DeepEquals(new preg_baby_name()))
                {
                    if (data.custom_baby_name_by_user_id != null)
                    {
                        data.custom_baby_name_by_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));
            }
        }
 public HttpResponseMessage Get([FromUri] preg_baby_name data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         IQueryable <preg_baby_name> result;
         if (!data.DeepEquals(new preg_baby_name()))
         {
             if (data.custom_baby_name_by_user_id != null)
             {
                 data.custom_baby_name_by_user_id = user_id;
             }
             result = dao.GetItemsByParams(data).Where(c => c.custom_baby_name_by_user_id == null || c.custom_baby_name_by_user_id == user_id);
         }
         else
         {
             result = dao.GetListItem().Where(c => c.custom_baby_name_by_user_id == null || c.custom_baby_name_by_user_id == user_id);
         }
         if (result.Any())
         {
             return(Request.CreateResponse(HttpStatusCode.OK, dao.FilterUserID(result, user_id)));
         }
         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));
     }
 }
        public HttpResponseMessage Put(string id, [FromBody] preg_baby_name dataUpdate)
        {
            try
            {
                int user_id = Convert.ToInt32(((ClaimsIdentity)User.Identity).FindFirst("id").Value);
                if (!dataUpdate.DeepEquals(new preg_baby_name()))
                {
                    preg_baby_name baby_name = new preg_baby_name();
                    baby_name = dao.GetItemByID(Convert.ToInt32(id)).FirstOrDefault();
                    if (baby_name == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
                    }
                    if (dataUpdate.country_id != null)
                    {
                        baby_name.country_id = dataUpdate.country_id;
                    }
                    if (dataUpdate.gender_id != null)
                    {
                        baby_name.gender_id = dataUpdate.gender_id;
                    }
                    if (dataUpdate.name != null)
                    {
                        baby_name.name = dataUpdate.name;
                    }
                    if (dataUpdate.custom_baby_name_by_user_id != null)
                    {
                        baby_name.custom_baby_name_by_user_id = user_id;
                    }
                    if (dataUpdate.order != null)
                    {
                        baby_name.order = dataUpdate.order;
                    }

                    dao.UpdateData(baby_name);
                    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 Post([FromBody] preg_user_baby_name data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         if (data.baby_name_id != 0)
         {
             data.user_id = user_id;
             //Check Exist
             preg_user_baby_name checkExist = dao.GetItemByID(user_id, data.baby_name_id).FirstOrDefault();
             if (checkExist != null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, SysConst.DATA_EXIST));
             }
             //Check babyname exist
             using (PregnancyEntity connect = new PregnancyEntity())
             {
                 preg_baby_name checkBabyNameExist = connect.preg_baby_name.Where(c => c.id == data.baby_name_id).FirstOrDefault();
                 if (checkBabyNameExist == null)
                 {
                     return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
                 }
             }
             if (dao.InsertData(data))
             {
                 return(Request.CreateResponse(HttpStatusCode.Created, SysConst.DATA_INSERT_SUCCESS));
             }
             else
             {
                 HttpError err = new HttpError(SysConst.DATA_EXIST);
                 return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
             }
         }
         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(string id)
 {
     try
     {
         int            user_id = Convert.ToInt32(((ClaimsIdentity)User.Identity).FindFirst("id").Value);
         preg_baby_name item    = dao.GetItemByID(Convert.ToInt32(id)).Where(c => c.custom_baby_name_by_user_id == null || c.custom_baby_name_by_user_id == user_id).FirstOrDefault();
         if (item == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
         }
         dao.DeleteData(item);
         return(Request.CreateResponse(HttpStatusCode.Accepted, SysConst.DATA_DELETE_SUCCESS));
     }
     catch (Exception ex)
     {
         HttpError err = new HttpError(ex.Message);
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, err));
     }
 }
示例#6
0
        public IQueryable <preg_baby_name> GetItemsByParams(preg_baby_name data)
        {
            IQueryable <preg_baby_name> results = connect.preg_baby_name;

            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)
                {
                    results = results.Where(c => c.id == (int)(propertyValue));
                }
                else if (propertyName == "country_id" && propertyValue != null)
                {
                    results = results.Where(c => c.country_id == (int)(propertyValue));
                }
                else if (propertyName == "gender_id" && propertyValue != null)
                {
                    results = results.Where(c => c.gender_id == (int)(propertyValue));
                }
                else if (propertyName == "name" && propertyValue != null)
                {
                    results = results.Where(c => SqlFunctions.PatIndex("%" + propertyValue.ToString() + "%", c.name) > 0);
                }
                else if (propertyName == "custom_baby_name_by_user_id" && propertyValue != null)
                {
                    results = results.Where(c => c.custom_baby_name_by_user_id == (int)(propertyValue));
                }
                else if (propertyName == "order" && propertyValue != null)
                {
                    results = results.Where(c => c.order == (int)(propertyValue));
                }
            }

            return(results);
        }
示例#7
0
 public void DeleteData(preg_baby_name item)
 {
     connect.preg_baby_name.Remove(item);
     connect.SaveChanges();
 }
示例#8
0
 public void UpdateData(preg_baby_name item)
 {
     connect.SaveChanges();
 }
示例#9
0
 public void InsertData(preg_baby_name item)
 {
     connect.preg_baby_name.Add(item);
     connect.SaveChanges();
 }