public async Task <ActionResult> ShareDirectory(int dirId, string userName) { using (IntelliDocsEntities db = new IntelliDocsEntities()) { AspNetUser otherUser = await db.AspNetUsers.Where(usr => usr.UserName == userName).FirstOrDefaultAsync(); if (otherUser == null) { return(Json(new { status = "error", message = "<strong>Error!</strong> The specified Username couldn't be found." })); } Share newShare = new Share() { shrCreatedDate = DateTime.Now, Directory_nodeId = dirId }; newShare.AspNetUsers.Add(otherUser); db.Shares.Add(newShare); try { await db.SaveChangesAsync(); } catch (Exception ex) { return(Json(new { status = "error", message = "<strong>Error!</strong> Failed to add the new Share: " + ex.Message })); } return(Json(new { status = "success", message = "<strong>Success!</strong> This Directory was successfully shared with " + otherUser.UserName + "." })); } }
public async Task <ActionResult> UnShareDocument(int docId, string userName) { using (IntelliDocsEntities db = new IntelliDocsEntities()) { AspNetUser otherUser = await db.AspNetUsers.Where(usr => usr.UserName == userName).FirstOrDefaultAsync(); if (otherUser == null) { return(Json(new { status = "error", message = "<strong>Error!</strong> The specified Username couldn't be found." })); } List <Share> docShares = await db.Shares.Where(share => share.Document_docId == docId && share.AspNetUsers.Select(usr => usr.UserName).Contains(userName)).ToListAsync(); foreach (Share shr in docShares) { List <AspNetUser> updatedUsers = shr.AspNetUsers.ToList(); updatedUsers.RemoveAll(usr => usr.UserName == userName); shr.AspNetUsers = updatedUsers; db.Entry(shr).State = EntityState.Modified; } try { await db.SaveChangesAsync(); } catch (Exception ex) { return(Json(new { status = "error", message = "<strong>Error!</strong> Failed to modify the Share: " + ex.Message })); } return(Json(new { status = "success", message = "<strong>Success!</strong> This document was un-successfully shared with " + otherUser.UserName + "." })); } }
/// <summary> /// Asynchronously creates a new Library entity for the specified User. /// </summary> /// <param name="userId">The User ID of the User for which to create a new Library.</param> /// <returns>The newly created Library entity.</returns> public static async Task <Library> CreateLibrary(string userId) { using (IntelliDocsEntities db = new IntelliDocsEntities()) { Library newLibrary = new Library() { AspNetUser_Id = userId, libCreatedDate = DateTime.Now }; db.Libraries.Add(newLibrary); try { await db.SaveChangesAsync(); } catch (Exception ex) { throw ex; } return(newLibrary); } }
/// <summary> /// Creates a new Directory record for the current User's Library with the specified Parent Directory and Name. /// </summary> /// <param name="parentDirId">The id of the new Directory's Parent.</param> /// <param name="dirName">The name of the new Directory.</param> /// <param name="libraryId">The ID of a specific Library to use if desired.</param> /// <returns>JSON specifying status and the ID of the new Directory if successful.</returns> public async Task <ActionResult> CreateFolder(int?parentDirId, string dirName, int?libraryId = null) { if (parentDirId == null) { return(Json(new { status = "error", message = "A Directory Parent ID is required." })); } if (parentDirId == 0) { parentDirId = null; } if (string.IsNullOrEmpty(dirName)) { return(Json(new { status = "error", message = "A Directory name is required." })); } string userId = User.Identity.GetUserId(); Directory newDir = new Directory() { dirCreateDate = DateTime.Now, dirName = dirName, dirParentId = parentDirId }; using (IntelliDocsEntities db = new IntelliDocsEntities()) { Library userLibrary; if (libraryId == null || libraryId == 0) { userLibrary = await User.Identity.GetLibraryAsync(); } else { userLibrary = await db.Libraries.FindAsync(libraryId); if (userLibrary.AspNetUser_Id != userId) { return(Json(new { status = "error", message = "The specified Library is not owned by the current User." })); } } newDir.Library_libId = userLibrary.libId; if (parentDirId == null) { newDir.dirParentId = userLibrary.RootDirectory.dirId; } db.Directories.Add(newDir); try { await db.SaveChangesAsync(); } catch (Exception ex) { // TODO: Exception handling return(Json(new { status = "error", message = "Error adding new Directory to DB: " + ex.Message })); } System.IO.Directory.CreateDirectory(Path.Combine(Server.MapPath("~"), newDir.Path)); } return(Json(new { status = "success", message = "New folder, " + newDir.dirName + ", was created <strong>successfully</strong>!" })); }