Exemplo n.º 1
0
        public async Task <IActionResult> AddModifyTrainingProgram(TrainingProgramHeadViewModel viewModel)
        {
            TrainingProgramHead trPrHead = new TrainingProgramHead();

            userId = userManager.GetUserId(HttpContext.User);
            int headId = viewModel.Id;

            if (viewModel != null)
            {
                if (viewModel.Id == 0)
                {
                    if (appContext.TrainingProgramHeads.Where(x => x.Name == viewModel.Name & x.Date == viewModel.Date & x.UserId == userId).FirstOrDefault() != null)
                    {
                        ModelState.AddModelError("Trainig program is existed", "Trainig program is existed");
                        return(RedirectToAction("List"));
                    }
                    ;
                    trPrHead = new TrainingProgramHead
                    {
                        Name        = viewModel.Name,
                        CategoryId  = viewModel.CategoryId,
                        Date        = viewModel.Date.ToUniversalTime(),
                        UserId      = userId,
                        Description = viewModel.Description,
                        Category    = new Category
                        {
                            Id   = viewModel.CategoryId,
                            Name = appContext.Categories.ToList().Where(x => x.Id == viewModel.CategoryId).FirstOrDefault().Name
                        }
                    };
                    await appContext.TrainingProgramHeads.AddAsync(trPrHead);

                    await appContext.SaveChangesAsync();

                    headId = trPrHead.Id;
                }
                else
                {
                    trPrHead = new TrainingProgramHead
                    {
                        Id          = headId,
                        CategoryId  = viewModel.CategoryId,
                        Name        = viewModel.Name,
                        Date        = viewModel.Date,
                        Description = viewModel.Description,
                        UserId      = userId
                    };
                    appContext.Update(trPrHead);
                    await appContext.SaveChangesAsync();
                }
                return(View("TrainingSpecList", Getmodel(headId)));
            }
            return(RedirectToAction("List"));
        }
Exemplo n.º 2
0
        public IActionResult AddModifyTrainingProgram(int id = 0)
        {
            TrainingProgramHeadViewModel trHeadViewModel = new TrainingProgramHeadViewModel
            {
                Date       = DateTime.Now.ToLocalTime(),
                Categories = appContext.Categories.ToList(),
                Exercises  = appContext.Exercises.ToList()
            };

            if (id != 0)
            {
                trHeadViewModel.Id          = id;
                trHeadViewModel.Name        = appContext.TrainingProgramHeads.Find(id).Name;
                trHeadViewModel.Date        = appContext.TrainingProgramHeads.Find(id).Date.ToLocalTime();
                trHeadViewModel.Description = appContext.TrainingProgramHeads.Find(id).Description;
                trHeadViewModel.CategoryId  = appContext.TrainingProgramHeads.Find(id).CategoryId;
            }
            return(View(trHeadViewModel));
        }