示例#1
0
        public async Task <IHttpActionResult> create(Models.GoalChecklistModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { error = true, message = Models.ErrorMessage.getErrorMessage(ModelState) }));
            }
            Boolean result = true;

            //string[] date = model.startDate.Split('/');
            try
            {
                Goal goal = new Goal();
                goal.goalName    = model.goalName;
                goal.description = model.description;
                // goal.startDate = DateTime.Parse(date[1] + "/" + date[0] + "/" + ( Convert.ToInt32(date[2])+543));
                goal.startDate  = model.startDate;
                goal.endDate    = model.endDate;
                goal.categoryID = model.categoryID;
                goal.circleID   = model.circleID;
                goal.userID     = User.Identity.GetUserId();
                goal.circleType = model.circleType;
                goal.flag       = model.flag;

                System.Web.HttpContext.Current.Application.Lock();
                _db.Goals.Add(goal);
                int isSave = _db.SaveChanges();
                if (isSave == 1)
                {
                    Goal g = _db.Goals.Where(p => p.goalName == model.goalName).FirstOrDefault();

                    List <DAL.Checklist> cList = new List <DAL.Checklist>();
                    foreach (var c in model.checklists)
                    {
                        DAL.Checklist checklist = new DAL.Checklist();
                        checklist.checklistName = c.value;
                        checklist.goalID        = g.id;
                        cList.Add(checklist);
                    }
                    _db.Checklists.AddRange(cList);
                    _db.SaveChanges();

                    List <GoalHandler> ghList = new List <GoalHandler>();


                    if (model.users != null)
                    {
                        foreach (var u in model.users)
                        {
                            GoalHandler gh = new GoalHandler();
                            gh.userID = u;
                            gh.goalID = g.id;
                            _db.GoalHandlers.Add(gh);
                            _db.SaveChanges();

                            var c = from cl in _db.Checklists where cl.goalID.Equals(g.id) select cl;
                            List <ChecklistProgress> clpList = new List <ChecklistProgress>();
                            foreach (var clp in c)
                            {
                                ChecklistProgress checklistProgress = new ChecklistProgress();
                                checklistProgress.checklistProgress1 = 1;
                                checklistProgress.time          = DateTime.Now;
                                checklistProgress.checklistID   = clp.id;
                                checklistProgress.goalHandlerID = gh.id;
                                clpList.Add(checklistProgress);
                            }
                            _db.ChecklistProgresses.AddRange(clpList);
                        }
                    }
                    else
                    {
                        GoalHandler gh = new GoalHandler();
                        gh.userID = User.Identity.GetUserId();
                        gh.goalID = g.id;
                        _db.GoalHandlers.Add(gh);
                        _db.SaveChanges();

                        var c = from cl in _db.Checklists where cl.goalID.Equals(g.id) select cl;
                        List <ChecklistProgress> clpList = new List <ChecklistProgress>();
                        foreach (var clp in c)
                        {
                            ChecklistProgress checklistProgress = new ChecklistProgress();
                            checklistProgress.checklistProgress1 = 1;
                            checklistProgress.time          = DateTime.Now;
                            checklistProgress.checklistID   = clp.id;
                            checklistProgress.goalHandlerID = gh.id;
                            clpList.Add(checklistProgress);
                        }
                        _db.ChecklistProgresses.AddRange(clpList);
                    }

                    _db.SaveChanges();
                }
                System.Web.HttpContext.Current.Application.UnLock();
            }
            catch (Exception e)
            {
                return(Json(e.Message));
            }

            return(Json(new { result = result }));
        }
示例#2
0
        public async Task <IHttpActionResult> update(Models.GoalChecklistModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { error = true, message = Models.ErrorMessage.getErrorMessage(ModelState) }));
            }
            Boolean result = true;

            try
            {
                System.Web.HttpContext.Current.Application.Lock();

                var goal = _db.Goals.Where(p => p.id == model.id).FirstOrDefault();
                goal.goalName    = model.goalName;
                goal.description = model.description;
                goal.startDate   = model.startDate;
                goal.endDate     = model.endDate;
                goal.categoryID  = model.categoryID;
                goal.circleID    = model.circleID;
                goal.userID      = User.Identity.GetUserId();
                goal.circleType  = model.circleType;


                System.Web.HttpContext.Current.Application.Lock();
                _db.SaveChanges();

                {
                    Goal g = _db.Goals.Where(p => p.id == model.id).FirstOrDefault();

                    var checklists = from cl in _db.Checklists where cl.goalID.Equals(g.id) select cl;
                    var checkused  = 0;
                    foreach (var checklist in checklists)
                    {
                        var checklistPs = from clp in _db.ChecklistProgresses where clp.checklistID.Equals(checklist.id) select clp;

                        foreach (var checklistP in checklistPs)
                        {
                            if (checklistP.checklistProgress1 == 2)
                            {
                                checkused = 1;
                                break;
                            }
                        }
                    }
                    if (checkused == 0)
                    {
                        foreach (var checklist in checklists)
                        {
                            var checklistPs = from clp in _db.ChecklistProgresses where clp.checklistID.Equals(checklist.id) select clp;
                            foreach (var checklistP in checklistPs)
                            {
                                _db.ChecklistProgresses.Remove(checklistP);
                            }
                            _db.Checklists.Remove(checklist);
                        }

                        var goalHandlers = from gh in _db.GoalHandlers where gh.goalID.Equals(g.id) select gh;
                        foreach (var goalHandler in goalHandlers)
                        {
                            _db.GoalHandlers.Remove(goalHandler);
                        }
                        _db.SaveChanges();
                    }

                    List <DAL.Checklist> cList = new List <DAL.Checklist>();

                    foreach (var c in model.checklists)
                    {
                        DAL.Checklist checklist = new DAL.Checklist();
                        checklist.checklistName = c.value;
                        checklist.goalID        = g.id;
                        cList.Add(checklist);
                    }
                    _db.Checklists.AddRange(cList);
                    _db.SaveChanges();

                    List <GoalHandler> ghList = new List <GoalHandler>();


                    if (model.users != null)
                    {
                        foreach (var u in model.users)
                        {
                            GoalHandler gh = new GoalHandler();
                            gh.userID = u;
                            gh.goalID = g.id;
                            _db.GoalHandlers.Add(gh);
                            _db.SaveChanges();

                            var c = from cl in _db.Checklists where cl.goalID.Equals(g.id) select cl;
                            List <ChecklistProgress> clpList = new List <ChecklistProgress>();
                            foreach (var clp in c)
                            {
                                ChecklistProgress checklistProgress = new ChecklistProgress();
                                checklistProgress.checklistProgress1 = 1;
                                checklistProgress.time          = DateTime.Now;
                                checklistProgress.checklistID   = clp.id;
                                checklistProgress.goalHandlerID = gh.id;
                                clpList.Add(checklistProgress);
                            }
                            _db.ChecklistProgresses.AddRange(clpList);
                        }
                    }
                    else
                    {
                        GoalHandler gh = new GoalHandler();
                        gh.userID = User.Identity.GetUserId();
                        gh.goalID = g.id;
                        _db.GoalHandlers.Add(gh);
                        _db.SaveChanges();

                        var c = from cl in _db.Checklists where cl.goalID.Equals(g.id) select cl;
                        List <ChecklistProgress> clpList = new List <ChecklistProgress>();
                        foreach (var clp in c)
                        {
                            ChecklistProgress checklistProgress = new ChecklistProgress();
                            checklistProgress.checklistProgress1 = 2;
                            checklistProgress.time          = DateTime.Now;
                            checklistProgress.checklistID   = clp.id;
                            checklistProgress.goalHandlerID = gh.id;
                            clpList.Add(checklistProgress);
                        }
                        _db.ChecklistProgresses.AddRange(clpList);
                    }

                    _db.SaveChanges();
                }
                System.Web.HttpContext.Current.Application.UnLock();
            }
            catch (Exception e)
            {
                return(Json(e.Message));
            }

            return(Json(new { result = result }));
        }