Beispiel #1
0
        public async Task <IActionResult> Edit(int id, EditConversationPlanDateViewModel editConversationPlanDate)
        {
            if (!_context.ConversationPlanDates.Any())
            {
                return(RedirectToAction("Create"));
            }
            var conversationPlanDate = await _context.ConversationPlanDates.FirstAsync(cpd => cpd.Id == id);

            if (conversationPlanDate == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var conversationPlanDateClaims =
                    await _context.ConversationPlanDateClaims
                    .Where(pdc => pdc.ConversationPlanDate == conversationPlanDate)
                    .Include(pdc => pdc.Group)
                    .Include(pdc => pdc.ConversationPlanDate)
                    .ToListAsync();

                foreach (var selectedGroup in editConversationPlanDate.SelectedGroups)
                {
                    if (conversationPlanDateClaims.All(pdc => pdc.Group.Id != selectedGroup))
                    {
                        _context.ConversationPlanDateClaims.Add(new ConversationPlanDateClaim
                        {
                            ConversationPlanDate = conversationPlanDate,
                            Group =
                                _context.ApplicationUserGroups.Single(g => g.Id == selectedGroup)
                        });
                    }
                }
                foreach (var conversationPlanDateClaim in conversationPlanDateClaims)
                {
                    if (
                        editConversationPlanDate.SelectedGroups.All(
                            pdc => pdc != conversationPlanDateClaim.Group.Id))
                    {
                        _context.ConversationPlanDateClaims.Remove(conversationPlanDateClaim);
                    }
                }
                conversationPlanDate.StartDate = DateTime.ParseExact(editConversationPlanDate.StartDate, "dd-mm-yyyy", null);
                conversationPlanDate.EndDate   = DateTime.ParseExact(editConversationPlanDate.EndDate, "dd-mm-yyyy", null);
                _context.Update(conversationPlanDate);

                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(editConversationPlanDate));
        }
Beispiel #2
0
        // GET: Schooladmin/ConversationPlanDates/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var conversationPlanDate = await _context.ConversationPlanDates.SingleOrDefaultAsync(m => m.Id == id);

            if (conversationPlanDate == null)
            {
                return(NotFound());
            }
            var conversationPlanDateClaimGroups =
                await _context.ConversationPlanDateClaims.Where(pdc => pdc.ConversationPlanDate == conversationPlanDate).Select(pdc => pdc.Group)
                .ToListAsync();

            var editConversationPlanDate = new EditConversationPlanDateViewModel
            {
                StartDate = conversationPlanDate.StartDate.ToString("dd-mm-yyyy"),
                EndDate   = conversationPlanDate.EndDate.ToString("dd-mm-yyyy"),
                Id        = id.Value
            };
            var groups          = _context.ApplicationUserGroups.ToList();
            var groupSelectList = new List <SelectListItem>();

            foreach (var group in groups)
            {
                SelectListItem selectListItem = new SelectListItem
                {
                    Text  = group.GroupName,
                    Value = group.Id.ToString()
                };
                if (
                    conversationPlanDateClaimGroups.Any(
                        pdcg => pdcg.Id == group.Id))
                {
                    selectListItem.Selected = true;
                }
                groupSelectList.Add(selectListItem);
            }
            editConversationPlanDate.Groups = groupSelectList;
            return(View(editConversationPlanDate));
        }
Beispiel #3
0
        public async Task <IActionResult> Edit(int id, EditConversationPlanDateViewModel editConversationPlanDate)
        {
//            if (!_context.ConversationPlanDates.Any())
//            {
//                return RedirectToAction("Create");
//            }
//            var conversationPlanDate = await _context.ConversationPlanDates.FirstAsync(cpd => cpd.Id == id);
//            if (conversationPlanDate == null)
//            {
//                return NotFound();
//            }
//
//            if (ModelState.IsValid)
//            {
//                var conversationPlanDateClaims =
//                    await _context.ConversationPlanDateClaims
//                        .Where(pdc => pdc.ConversationPlanDate == conversationPlanDate)
//                        .Include(pdc => pdc.Group)
//                        .Include(pdc => pdc.ConversationPlanDate)
//                        .ToListAsync();
//                var modelGroups = new List<ApplicationUserGroup>();
//                foreach (var group in editConversationPlanDate.SelectedGroups)
//                {
//                    modelGroups.Add(_context.ApplicationUserGroups.First(g => g.Id == group));
//                }
//                var removeGroups = conversationPlanDateClaims.Select(pdc => pdc.Group).ToList().Except(modelGroups).ToList();
//                foreach (var group in removeGroups)
//                {
//                    if (!_context.Conversations.Include(c => c.Group).Any(c => c.Group == group))
//                    {
//                        var conversationTypeClaim =
//                            _context.ConversationTypeClaims.SingleOrDefault(ctc => ctc.ConversationType == conversationType && ctc.Group == group);
//                        if (conversationTypeClaim != null) // It shouldn't be null but just incase
//                            _context.ConversationTypeClaims.Remove(conversationTypeClaim);
//                    }
//                }
//                var AddGroups = modelGroups.Except(conversationTypeClaimsGroups).ToList();
//                foreach (var group in AddGroups)
//                {
//                    _context.ConversationTypeClaims.Add(new ConversationTypeClaim
//                    {
//                        ConversationType = conversationType,
//                        Group = group
//                    });
//                }
//
//                foreach (var selectedGroup in editConversationPlanDate.SelectedGroups)
//                {
//                    if (conversationPlanDateClaims.All(pdc => pdc.Group.Id != selectedGroup))
//                    {
//                        _context.ConversationPlanDateClaims.Add(new ConversationPlanDateClaim
//                        {
//                            ConversationPlanDate = conversationPlanDate,
//                            Group =
//                                _context.ApplicationUserGroups.Single(g => g.Id == selectedGroup)
//                        });
//                    }
//                }
//                foreach (var conversationPlanDateClaim in conversationPlanDateClaims)
//                {
//                    if (
//                        editConversationPlanDate.SelectedGroups.All(
//                            pdc => pdc != conversationPlanDateClaim.Group.Id))
//                    {
//                        _context.ConversationPlanDateClaims.Remove(conversationPlanDateClaim);
//                    }
//                }
//                conversationPlanDate.StartDate = DateTime.ParseExact(editConversationPlanDate.StartDate, "dd-mm-yyyy", null);
//                conversationPlanDate.EndDate = DateTime.ParseExact(editConversationPlanDate.EndDate, "dd-mm-yyyy", null);
//                _context.Update(conversationPlanDate);
//
//                await _context.SaveChangesAsync();
//                return RedirectToAction("Index");
//            }
            return(View(editConversationPlanDate));
        }