public async Task <ServiceResult <CourseEntry> > Add(Course course) { course.AuthorId = authService.GetUserId(); await dbContext.Courses.AddAsync(course); await dbContext.SaveChangesAsync(); return(await Get(course.Id)); }
public async Task <ServiceResult <NoteModel> > Add(NoteInput input) { authService.AssertAuthentication(); var tags = new List <Tag>(); var note = mapper.Map <Note>(input); note.Title = whitespace.Replace(note.Title.Trim(), " "); if (note.Title.Length < 10) { return(BadRequest <NoteModel>()); // format this somehow to convention } if (note.ParentId != null) { var parent = await dbContext.Notes.FindAsync(note.ParentId); if (parent == null) { return(BadRequest <NoteModel>()); } if (parent.AuthorId != authService.GetUserId()) { return(Unauthorized <NoteModel>()); } note.RootId = parent.IsRoot ? note.ParentId : parent.RootId; tags.AddRange(parent.Tags); // parents tags can be excluded here } else { note.IsRoot = true; } tags.AddRange(await ConvertTags(note.Tags)); note.Tags = tags; note.AuthorId = authService.GetUserId(); await dbContext.Notes.AddAsync(note); await dbContext.SaveChangesAsync(); note.Url = whitespace.Replace(note.Title, "-").ToLowerInvariant() + "-" + note.Id; await dbContext.SaveChangesAsync(); return(await Get(note.Id)); }
public async Task <ServiceResult> Remove(int id) { var question = await dbContext.Questions.FindAsync(id); if (question == null) { return(NotFound()); } if (question.Note.AuthorId != authService.GetUserId()) { return(Unauthorized()); } dbContext.Questions.Remove(question); await dbContext.SaveChangesAsync(); return(Ok()); }