private void ProcessPageNotes(VideoCreate model) { for (int i = 0; i < model.Notes.Count; i++) { if (model.Notes[i].Content == null) { model.Notes[i].Content = string.Empty; } } }
public IActionResult Create([FromBody] VideoCreate data) { try { var userData = jwtService.ParseData(this.User); VideoIndex result = videoService.Create(data, userData.UserId); return(Ok(result)); } catch (ServiceException e) { return(BadRequest(e.Message)); } }
// Create public bool AddVideo(VideoCreate model) { var entity = new Video() { UserId = _userId, Description = model.Description, URL = ReformatYoutubeURL(model.URL), WordId = _wordId }; _context.Videos.Add(entity); return(_context.SaveChanges() == 1); }
public ActionResult Create(int languageId, int wordId, VideoCreate model) { if (!ModelState.IsValid) { return(View(model)); } var service = CreateVideoService(wordId); if (service.AddVideo(model)) { TempData["SaveResult"] = "Video was added successfully."; return(RedirectToAction("Details", "Word", new { languageId, id = wordId })); } ModelState.AddModelError("", "Video was not added."); return(View(model)); }
[Test]///Checked public void GetVideoForEditShouldReturnCorrectResult() { UserS.SeedPeshoAndGosho(context); var video = VideoS.SeedVideosToUserWithNotes(context, UserS.GoshoId, true); Func <VideoCreate> function = () => this.videoService.GetVideoForEdit(video.Id, UserS.GoshoUsername); var result = function.Invoke(); var dbNote0 = video.Notes.SingleOrDefault(x => x.Order == 0); var dbNote1 = video.Notes.SingleOrDefault(x => x.Order == 1); var dbNote2 = video.Notes.SingleOrDefault(x => x.Order == 2); var pageNote0 = MapVideoNoteToNoteCreate(dbNote0); var pageNote1 = MapVideoNoteToNoteCreate(dbNote1); var pageNote2 = MapVideoNoteToNoteCreate(dbNote2); pageNote0.Level = 1; pageNote1.Level = 2; pageNote2.Level = 1; pageNote1.InPageParentId = pageNote0.InPageId; var expectedResult = new VideoCreate { Description = video.Description, DirectoryId = video.DirectoryId, Id = video.Id, Name = video.Name, Order = video.Order, SeekTo = video.SeekTo, Url = video.Url, Notes = new List <VideoNoteCreate> { pageNote0, pageNote1, pageNote2 } }; result.Should().BeEquivalentTo(expectedResult); }
/// 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); }
public VideoIndex Create(VideoCreate videoCreate, int userId, bool isAdmin = false) { var dirId = videoCreate.DirectoryId; var user = context.Users.SingleOrDefault(x => x.Id == userId); if (user == null) { throw new ServiceException("User Not Found!"); } //-1 means add it to the root; if (dirId == -1) { dirId = this.context.Directories .Where(x => x.UserId == user.Id && x.ParentDirectoryId == null) .Single() .Id; } var directory = context.Directories.SingleOrDefault(x => x.Id == dirId); if (directory == null) { throw new ServiceException("The Directory you selected for creating the new video notes in, does not exist!"); } if (user.Id != directory.UserId && isAdmin == false) { throw new ServiceException("The directory you are trying to create a video on does note belong to you!"); } var typeBools = new bool[] { videoCreate.IsLocal, videoCreate.IsYouTube, videoCreate.IsVimeo }; if (typeBools.Where(x => x).Count() != 1) { throw new ServiceException("You must select one type of video!"); } var ordersInfo = context.Directories .Select(x => new { id = x.Id, orders = x.Videos.Select(y => y.Order).ToArray() }) .SingleOrDefault(x => x.id == dirId); var order = ordersInfo.orders.Length == 0 ? 0 : ordersInfo.orders.Max() + 1; var video = new Video { DirectoryId = dirId, Order = order, UserId = user.Id, Name = videoCreate.Name, Description = videoCreate.Description, Url = videoCreate.Url, IsLocal = videoCreate.IsLocal, IsYouTube = videoCreate.IsYouTube, IsVimeo = videoCreate.IsVimeo, SeekTo = 0, }; context.Videos.Add(video); context.SaveChanges(); var index = Mapper.Instance.Map <VideoIndex>(video); return(index); }