public UserSettings CreateSettings(string username) { var settings = new UserSettings(); var user = userService.ByUsername(username); user.UserSettings = settings; context.SaveChanges(); return(settings); }
public void Create(string userName, string name, List <ListRemindItemCreate> listItems) { var userId = context.Users.SingleOrDefault(x => x.UserName == userName).Id; var list = CreateList(userId, name, listItems); context.ListsRemind.Add(list); context.SaveChanges(); }
public static Comparison[] SeedTwoCompsToUser(MomentoDbContext context, string userId) { var user = context.Users.SingleOrDefault(x => x.Id == userId); var comps = new List <Comparison>(); comps.Add(new Comparison { Description = "", Name = "", Order = 0, TargetLanguage = "", SourceLanguage = "", UserId = user.Id, DirectoryId = user.Directories.SingleOrDefault().Id, }); comps.Add(new Comparison { Description = "", Name = "", Order = 0, TargetLanguage = "", SourceLanguage = "", UserId = user.Id, DirectoryId = user.Directories.SingleOrDefault().Id, }); context.AddRange(comps); context.SaveChanges(); return(comps.ToArray()); }
public static ComparisonItem[] SeedThreeItemsToComp(MomentoDbContext context, Comparison comp) { var items = new HashSet <ComparisonItem> { new ComparisonItem { Id = 1, Comment = "item1Comment", Order = 1, Target = "item1Target", Source = "item1Source", }, new ComparisonItem { Id = 2, Comment = "item2Comment", Order = 2, Target = "item2Target", Source = "item2Source", }, new ComparisonItem { Id = 3, Comment = "item3Comment", Order = 3, Target = "item3Target", Source = "item3Source", }, }; comp.Items = items; context.SaveChanges(); return(items.ToArray()); }
/// <summary> /// Also seeds their root directories! /// </summary> public static void SeedPeshoAndGosho(MomentoDbContext context) { var users = new User[] { new User { Id = PeshoId, FirstName = "Pesho", LastName = "Peshov", UserName = PeshoUsername, Email = "*****@*****.**", Directories = new Directory[] { new Directory { Name = PeshoRootDir, Id = PeshoRootDirId } } }, new User { Id = GoshoId, FirstName = "Gosho", LastName = "Goshov", UserName = GoshoUsername, Email = "*****@*****.**", Directories = new Directory[] { new Directory { Name = GoshoRootDir, Id = GoshoRootDirId } } }, }; context.Users.AddRange(users); context.SaveChanges(); }
public static CodeLine[] SeedTwoCodeLinesToNote(Note note, MomentoDbContext context) { var line1 = new CodeLine { EditorMode = line1EditorMode, InPageId = line1InPageId, NoteContent = line1NoteContent, Visible = line1Visible, Order = line1Order, SourceContent = line1SourceContent, Note = note, }; var line2 = new CodeLine { EditorMode = line2EditorMode, InPageId = line2InPageId, NoteContent = line2NoteContent, Visible = line2Visible, Order = line2Order, SourceContent = line2SourceContent, Note = note, }; var lines = new CodeLine[] { line1, line2 }; context.CodeLines.AddRange(lines); context.SaveChanges(); return(lines); }
public void SoftCascadeDelete(ISoftDeletable model) { var deleteTime = DateTime.UtcNow; this.SoftCascadeDelete(model, deleteTime); context.SaveChanges(); }
public static Video SeedVideosToUserWithNotes(MomentoDbContext context, string userId, bool nestedNote = false) { var user = context.Users.SingleOrDefault(x => x.Id == userId); var rootDirectoryId = user.Directories.FirstOrDefault(x => x.Name.Contains("Root")).Id; var note1 = new VideoNote { Order = 0, Id = preExistingNote1Id, Content = preExistingNote1Content, Formatting = DefaultNoteFormatting, SeekTo = DefaultNoteSeekTo, }; var note2 = new VideoNote { Order = 2, Id = preExistingNote2Id, Content = preExistingNote2Content, Formatting = DefaultNoteFormatting, SeekTo = DefaultNoteSeekTo, }; var video = new Video { UserId = userId, Name = DefaultVideoName, Description = DefaultVideoDesctiption, SeekTo = DefaultVideoSeekTo, Url = DefaultVideoUrl, DirectoryId = rootDirectoryId, Notes = new HashSet <VideoNote> { note1, note2, } }; if (nestedNote) { var note3 = new VideoNote { Order = 1, Id = preExistingNote3Id, Content = preExistingNote3Content, Formatting = DefaultNoteFormatting, SeekTo = DefaultNoteSeekTo, Video = video, }; note1.ChildNotes = new HashSet <VideoNote> { note3 }; } user.Videos.Add(video); context.SaveChanges(); return(video); }
//Verified public int Create(int parentDirId, string dirName, string username) { var parentDir = context.Directories.SingleOrDefault(x => x.Id == parentDirId); if (parentDir == null) { throw new ItemNotFound("The parent directory of the directory you are trying to create does not exist!"); } var user = this.context.Users.SingleOrDefault(x => x.UserName == username); if (user == null) { throw new UserNotFound(username); } if (parentDir.UserId != user.Id) { throw new AccessDenied("The parent directory does not belong to you!"); } if (string.IsNullOrWhiteSpace(dirName) || dirName.Length == 0 || dirName.ToLower() == "root") { throw new BadRequestError("The directory name is not valid!"); } var order = 0; if (parentDir.Subdirectories.Any()) { order = parentDir.Subdirectories.Select(x => x.Order).Max() + 1; } var dir = new Directory { Name = dirName, UserId = parentDir.UserId, ParentDirectory = parentDir, Order = order, }; context.Directories.Add(dir); context.SaveChanges(); return(dir.Id); }
public void Create(ComparisonCreate data, string username) { var parentDirId = data.ParentDirId; var user = this.context.Users.SingleOrDefault(x => x.UserName == username); if (user == null) { throw new UserNotFound(username); } var parentDir = this.context.Directories.SingleOrDefault(x => x.Id == parentDirId); if (parentDir == null) { throw new ItemNotFound("The parent directory for the comparison you are trying to create does not exist!"); } ///Check the Dir belongs to the user if (parentDir.UserId != user.Id) { throw new AccessDenied("The directory you are trying to put the comparison in does not belong to you!"); } var numberOfExistingComparisonsInGivenDirectory = this.context.Directories. Where(x => x.Id == parentDirId) .Select(x => x.Comparisons.Count) .SingleOrDefault(); var order = numberOfExistingComparisonsInGivenDirectory; var comparison = new Comparison { UserId = user.Id, DirectoryId = parentDir.Id, Order = order, Name = data.Name, Description = data.Description, TargetLanguage = data.TargetLanguage, SourceLanguage = data.SourceLanguage, }; context.Comparisons.Add(comparison); context.SaveChanges(); }
public void Create(CodeCreate model) { model.Id = null; var dbCode = mapper.Map <Code>(model); dbCode.CodeHashtags = ParseCodeHashtags <CodeHashtag>(model.Hashtag); foreach (var note in dbCode.Notes) { note.CodeNoteHashtags = ParseCodeHashtags <CodeNoteHashtag>(note.Hashtags); } context.Code.Add(dbCode); context.SaveChanges(); }
public void UploadData(string json, int parentDir) { json = string.Join(Environment.NewLine, json.Split(Environment.NewLine).Skip(1)); var directory = (Directory)JsonConvert.DeserializeObject(json, typeof(Directory)); FixIds(directory); directory.ParentDirectory = null; directory.ParentDirectoryId = parentDir; context.Directories.Add(directory); context.SaveChanges(); }
public void RegisterModification(ITrackable item, DateTime now, bool saveChanges) { item.LastModifiedOn = now; item.TimesModified += 1; if (saveChanges) { context.SaveChanges(); } }
//Authenticated //Registers user for the list public void Create(ListToDoCreate pageListToDo, string username) { var user = context.Users.SingleOrDefault(x => x.UserName == username); if (user == null) { throw new UserNotFound(username); } var direcotory = context.Directories.SingleOrDefault(x => x.Id == pageListToDo.DirectoryId); if (direcotory == null) { throw new ItemNotFound("The directory for this List does not exists"); } if (direcotory.UserId != user.Id) { throw new AccessDenied("The directory you are trying to create you List does not belong to you!"); } var order = 0; var ids = direcotory.ListsToDo.Select(x => x.Order).ToArray(); if (ids.Length != 0) { order = ids.Max() + 1; } var newListToDo = Mapper.Instance.Map <ListToDo>(pageListToDo); newListToDo.Order = order; newListToDo.Id = default(int); newListToDo.UserId = user.Id; context.ListsTodo.Add(newListToDo); context.SaveChanges(); }
public Note Create(NoteCreate note, string username) { var user = context.Users.SingleOrDefault(x => x.UserName == username); if (user == null) { throw new UserNotFound(username); } var directory = context.Directories.SingleOrDefault(x => x.Id == note.DirectoryId); if (directory == null) { throw new ItemNotFound("The directory you are trying to add a note to does not exist!"); } if (user.Id != directory.UserId) { throw new AccessDenied("The directory you are trying to add a note to des not exist!"); } var order = directory.Notes.Count == 0 ? 0 : directory.Notes.Select(x => x.Order).Max() + 1; var resultNote = new Note { Name = note.Name, Description = note.Description, Order = order, DirectoryId = note.DirectoryId, UserId = user.Id, }; context.Notes.Add(resultNote); context.SaveChanges(); return(resultNote); }
public static Video SeedVideosToUser(MomentoDbContext context, string userId) { var user = context.Users.SingleOrDefault(x => x.Id == userId); var directoryId = user.Directories.FirstOrDefault(x => x.Name.Contains("Root")).Id; var video = new Video { DirectoryId = directoryId, UserId = userId, Name = "TestVideo1", }; context.Videos.Add(video); context.SaveChanges(); return(video); }
public void SaveItemsForOneDir(int parentDir, string cntOrDir, int[] orderings) { if (cntOrDir == "dir") { var directories = context.Directories .Include(x => x.Subdirectories) .SingleOrDefault(x => x.Id == parentDir) .Subdirectories .ToArray(); for (int i = 0; i < orderings.Length; i++) { var directoryId = orderings[i]; var dir = directories.SingleOrDefault(x => x.Id == directoryId); if (dir.Order != i) { dir.Order = i; } } } else if (cntOrDir == "cnt") { var contents = context.Directories .Include(x => x.Videos) .SingleOrDefault(x => x.Id == parentDir) .Videos .ToArray(); for (int i = 0; i < orderings.Length; i++) { var contentId = orderings[i]; var cnt = contents.SingleOrDefault(x => x.Id == contentId); if (cnt.Order != i) { cnt.Order = i; } } } context.SaveChanges(); }
public static Note SeedNoteToUser(string userId, MomentoDbContext context) { var user = context.Users.SingleOrDefault(x => x.Id == userId); var note = new Note { Description = NoteDesctiption, Directory = user.Directories.First(), Lines = new HashSet <CodeLine>(), MainNoteContent = NoteMainNoteContent, Name = NoteName, EditorMode = false, Order = 0, User = user, ShowSourceEditor = false, Source = "", }; context.Notes.Add(note); context.SaveChanges(); return(note); }
/// Verified that the video belongs to the user /// Registered viewing here /// <summary> /// How the noteCreate items are made: /// 1) Create a VideoNoteCreate array the same length as the array with dbNotes /// 2) Go through the dbNotes and copy all the non-relational data to the pageNotes /// -assign inPageIds to i /// -calculate level /// -In a Dictionary (map) map the dbNote.Id to i /// 3) Go through the Notes in pairs where one is the DbNote and the other is the page version /// -assing InPageParentId for the pageMpte to be what is mapped to the dbNote parent note Id /// if there is a parent note. /// </summary> /// Tested public VideoCreate GetVideoForEdit(int videoId, string username) { var video = context.Videos .Include(x => x.Notes) .SingleOrDefault(x => x.Id == videoId); if (video == null) { throw new ItemNotFound("The video you are trying to edit does not exist!"); } var userId = context.Users.SingleOrDefault(x => x.UserName == username)?.Id; if (userId == null) { throw new UserNotFound(username); } ///TODO: Videos should have users, remove when they do. if (video.UserId != null) { if (video.UserId != userId) { throw new AccessDenied("You can note edit video that does not belong to you!"); } } trackableService.RegisterViewing(video, DateTime.UtcNow, true); ///Reordering the notes so that when there are deletions the numbers are sequential var dbNotes = video.Notes.OrderBy(x => x.Order).ToArray(); for (int i = 0; i < dbNotes.Length; i++) { dbNotes[i].Order = i; } context.SaveChanges(); var map = new Dictionary <int, int>(); var pageNotes = new VideoNoteCreate[dbNotes.Length]; for (int i = 0; i < dbNotes.Length; i++) { var dbNote = dbNotes[i]; pageNotes[i] = new VideoNoteCreate { Id = dbNote.Id, Content = dbNote.Content, Formatting = dbNote.Formatting, Type = dbNote.Type, InPageId = dbNote.Order, SeekTo = dbNote.SeekTo, Level = Level(dbNote), }; map.Add(dbNote.Id, i); } for (int i = 0; i < dbNotes.Length; i++) { var dbNote = dbNotes[i]; var pageNote = pageNotes[i]; if (dbNote.NoteId != null) { pageNote.InPageParentId = map[(int)dbNote.NoteId]; } } var result = new VideoCreate { Id = video.Id, Description = video.Description, Url = video.Url, Name = video.Name, DirectoryId = video.DirectoryId, Order = video.Order, SeekTo = video.SeekTo, Notes = pageNotes.ToList(), }; return(result); }