Пример #1
0
        public async Task <IActionResult> CreateFolder(FlashcardFolderInfoViewModel flashcardFolderInfoViewModel)
        {
            if (ModelState.IsValid)
            {
                var folder = new FlashcardFolder
                {
                    FolderName  = flashcardFolderInfoViewModel.Name,
                    Subject     = flashcardFolderInfoViewModel.Subject,
                    Description = flashcardFolderInfoViewModel.Description,
                    Id          = flashcardFolderInfoViewModel.FlashcardFolderId
                };
                var success = _folderRepository.CreateFolder(folder);
                if (success)
                {
                    var user = await _userManager.GetUserAsync(HttpContext.User);

                    //probably want to change user.flashcardfolderids to a list, this is messy and confusing
                    //

                    if (user.FlashcardFolderIds != null)
                    {
                        var oldIds = user.FlashcardFolderIds;

                        // If the user deletes all their folderIds, the database leaves an empty string in their folderId column.
                        // (Probably bc of serialization of the enumerable)
                        // Replacing user.FlashcardFolderIds that has "" at index 0 is the same as starting a new list.
                        if (oldIds[0] == "")
                        {
                            var newIds = new List <string> {
                                flashcardFolderInfoViewModel.FlashcardFolderId
                            };
                            user.FlashcardFolderIds = newIds;
                        }
                        else
                        {
                            user.FlashcardFolderIds.Add(flashcardFolderInfoViewModel.FlashcardFolderId);
                        }
                    }
                    else
                    {
                        user.FlashcardFolderIds = new List <string> {
                            flashcardFolderInfoViewModel.FlashcardFolderId
                        };
                    }


                    await _userManager.UpdateAsync(user);

                    return(RedirectToAction("CreateFolderSuccess", new { Id = flashcardFolderInfoViewModel.FlashcardFolderId }));
                }
            }
            TempData["ErrorMessage"] = "Something happened and we couldn't create that folder.  Make sure you filled in every field, then try again.";
            return(RedirectToAction("CreateFolder", new { Id = flashcardFolderInfoViewModel.FlashcardFolderId }));
        }
        public bool UpdateFolder(FlashcardFolder folder)
        {
            var targetFolder = _appDbContext.FlashcardFolders
                               .FirstOrDefault(f => f.Id == folder.Id);

            if (targetFolder != null)
            {
                targetFolder.FolderName  = folder.FolderName;
                targetFolder.Subject     = folder.Subject;
                targetFolder.Description = folder.Description;
                _appDbContext.SaveChanges();
                return(true);
            }
            return(false);
        }
        public bool CreateFolder(FlashcardFolder folder)
        {
            var nullOrCollision = _appDbContext.FlashcardFolders
                                  .FirstOrDefault(f => f.Id == folder.Id);

            if (nullOrCollision == null)
            {
                var newFolder = new FlashcardFolder
                {
                    FolderName  = folder.FolderName,
                    Subject     = folder.Subject,
                    Description = folder.Description,
                    Id          = folder.Id
                };
                _appDbContext.FlashcardFolders.Add(newFolder);

                _appDbContext.SaveChanges();
                return(true);
            }
            return(false);
        }
Пример #4
0
        public IActionResult EditFolder(FlashcardFolderInfoViewModel flashcardFolderInfoViewModel)
        {
            if (ModelState.IsValid)
            {
                var folder = new FlashcardFolder
                {
                    FolderName  = flashcardFolderInfoViewModel.Name,
                    Subject     = flashcardFolderInfoViewModel.Subject,
                    Description = flashcardFolderInfoViewModel.Description,
                    Id          = flashcardFolderInfoViewModel.FlashcardFolderId
                };

                var success = _folderRepository.UpdateFolder(folder);

                if (success)
                {
                    TempData["UserMessage"] = "Folder updated!";
                    return(RedirectToAction("FolderDetails", new { Id = flashcardFolderInfoViewModel.FlashcardFolderId }));
                }
            }
            ModelState.AddModelError("EditFolderFailed", "Unable to make your changes. Please make sure every field is filled in, then try again.");
            return(View(flashcardFolderInfoViewModel));
        }