public HttpResponseMessage Put(string todo_id, [FromBody] preg_user_todo dataUpdate)
        {
            try
            {
                int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
                if (!dataUpdate.DeepEquals(new preg_user_todo()))
                {
                    preg_user_todo user = new preg_user_todo();
                    user = dao.GetItemByID(user_id, Convert.ToInt32(todo_id)).FirstOrDefault();
                    if (user == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
                    }

                    if (dataUpdate.status != null)
                    {
                        user.status = dataUpdate.status;
                    }
                    dao.UpdateData(user);
                    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_todo data)
        {
            try
            {
                int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
                if (data.todo_id != 0)
                {
                    //Check exist
                    preg_user_todo checkExist = dao.GetItemByParams(new preg_user_todo()
                    {
                        user_id = user_id, todo_id = data.todo_id
                    }).FirstOrDefault();
                    if (checkExist != null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, SysConst.DATA_EXIST));
                    }

                    //Check Todo Exist
                    using (PregnancyEntity connect = new PregnancyEntity())
                    {
                        preg_todo checkTodoExist = connect.preg_todo.Where(c => c.id == data.todo_id).FirstOrDefault();
                        if (checkTodoExist == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.NotFound, SysConst.DATA_NOT_FOUND));
                        }
                    }

                    data.user_id = user_id;
                    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));
            }
        }
Ejemplo n.º 3
0
        public bool InsertData(preg_user_todo item)
        {
            IEnumerable <preg_user_todo> result = GetItemByParams(item);

            if (result.Count() > 0)
            {
                return(false);
            }
            else
            {
                connect.preg_user_todo.Add(item);
                connect.SaveChanges();
                return(true);
            }
        }
        public HttpResponseMessage Delete(string todo_id)
        {
            try
            {
                int            user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
                preg_user_todo item    = dao.GetItemByID(user_id, Convert.ToInt32(todo_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 todo_id)
 {
     try
     {
         int            user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         preg_user_todo data    = dao.GetItemByID(user_id, Convert.ToInt32(todo_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 Get([FromUri] preg_user_todo data)
 {
     try
     {
         int user_id = Convert.ToInt32(((ClaimsIdentity)(User.Identity)).FindFirst("id").Value);
         if (!data.DeepEquals(new preg_user_todo()))
         {
             data.user_id = user_id;
             IEnumerable <preg_user_todo> result = dao.GetItemByParams(data);
             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));
             }
         }
         else
         {
             IEnumerable <preg_user_todo> 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));
     }
 }
Ejemplo n.º 7
0
        public IQueryable <preg_user_todo> GetItemByParams(preg_user_todo data)
        {
            IQueryable <preg_user_todo> result = connect.preg_user_todo;

            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 == "user_id" && (int)propertyValue != 0)
                {
                    result = result.Where(c => c.user_id == (int)(propertyValue));
                }
                else if (propertyName == "todo_id" && (int)propertyValue != 0)
                {
                    result = result.Where(c => c.todo_id == (int)(propertyValue));
                }
                else if (propertyName == "status" && propertyValue != null)
                {
                    result = result.Where(c => c.status == (int)(propertyValue));
                }
            }
            return(result);
        }
Ejemplo n.º 8
0
 public void DeleteData(preg_user_todo item)
 {
     connect.preg_user_todo.Remove(item);
     connect.SaveChanges();
 }
Ejemplo n.º 9
0
 public void UpdateData(preg_user_todo item)
 {
     connect.SaveChanges();
 }