public HttpResponseMessage Get([FromUri] preg_my_weight data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         IEnumerable <preg_my_weight> result;
         if (!data.DeepEquals(new preg_my_weight()))
         {
             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));
     }
 }
        public HttpResponseMessage Put(string id, [FromBody] preg_my_weight dataUpdate)
        {
            try
            {
                int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
                if (!dataUpdate.DeepEquals(new preg_my_weight()))
                {
                    preg_my_weight my_weight = new preg_my_weight();
                    my_weight = dao.GetItemByID(Convert.ToInt32(id)).Where(c => c.user_id == user_id).FirstOrDefault();
                    if (my_weight == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
                    }
                    if (dataUpdate.my_weight_type_id != null)
                    {
                        my_weight.my_weight_type_id = dataUpdate.my_weight_type_id;
                    }
                    if (dataUpdate.start_date != null)
                    {
                        my_weight.start_date = dataUpdate.start_date;
                    }
                    if (dataUpdate.pre_pregnancy_weight != null)
                    {
                        my_weight.pre_pregnancy_weight = dataUpdate.pre_pregnancy_weight;
                    }
                    if (dataUpdate.current_weight != null)
                    {
                        my_weight.current_weight = dataUpdate.current_weight;
                    }
                    if (dataUpdate.week_id != null)
                    {
                        my_weight.week_id = dataUpdate.week_id;
                    }
                    if (dataUpdate.current_date != null)
                    {
                        my_weight.current_date = dataUpdate.current_date;
                    }

                    dao.UpdateData(my_weight);
                    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));
            }
        }
Exemplo n.º 3
0
        public IQueryable <preg_my_weight> GetItemsByParams(preg_my_weight data)
        {
            IQueryable <preg_my_weight> result = connect.preg_my_weight;

            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 == "my_weight_type_id" && propertyValue != null)
                {
                    result = result.Where(c => c.my_weight_type_id == (int)(propertyValue));
                }
                else if (propertyName == "start_date" && propertyValue != null)
                {
                    result = result.Where(c => c.start_date == (DateTime)(propertyValue));
                }
                else if (propertyName == "pre_pregnancy_weight" && propertyValue != null)
                {
                    result = result.Where(c => c.pre_pregnancy_weight == (double)(propertyValue));
                }
                else if (propertyName == "current_weight" && propertyValue != null)
                {
                    result = result.Where(c => c.current_weight == (double)(propertyValue));
                }
                else if (propertyName == "week_id" && propertyValue != null)
                {
                    result = result.Where(c => c.week_id == (int)(propertyValue));
                }
                else if (propertyName == "current_date" && propertyValue != null)
                {
                    result = result.Where(c => c.current_date == (DateTime)(propertyValue));
                }
            }
            return(result);
        }
 public HttpResponseMessage Delete(string id)
 {
     try
     {
         int            user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         preg_my_weight item    = dao.GetItemByID(Convert.ToInt32(id)).Where(c => c.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));
     }
 }
 public HttpResponseMessage Get(string id)
 {
     try
     {
         int            user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         preg_my_weight data    = dao.GetItemByID(Convert.ToInt32(id)).Where(c => c.user_id == user_id).FirstOrDefault();
         if (data != null)
         {
             return(Request.CreateResponse(HttpStatusCode.OK, data));
         }
         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 Post([FromBody] preg_my_weight data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         if (!data.DeepEquals(new preg_my_weight()))
         {
             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));
     }
 }
Exemplo n.º 7
0
 public void DeleteData(preg_my_weight item)
 {
     connect.preg_my_weight.Remove(item);
     connect.SaveChanges();
 }
Exemplo n.º 8
0
 public void UpdateData(preg_my_weight item)
 {
     connect.SaveChanges();
 }
Exemplo n.º 9
0
 public void InsertData(preg_my_weight item)
 {
     connect.preg_my_weight.Add(item);
     connect.SaveChanges();
 }