public async Task <object> Create([FromBody] TrickForm trickForm) { var trick = new Trick { Slug = trickForm.Name.Replace(" ", "-").ToLowerInvariant(), Name = trickForm.Name, Version = 1, Description = trickForm.Description, Difficulty = trickForm.Difficulty, TrickCategories = trickForm.Categories.Select(x => new TrickCategory { CategoryId = x }).ToList() }; _ctx.Add(trick); await _ctx.SaveChangesAsync(); _ctx.Add(new ModerationItem { Target = trick.Id, Type = ModerationTypes.Trick, }); await _ctx.SaveChangesAsync(); return(TrickViewModels.Create(trick)); }
public async Task <object> Update([FromBody] Trick trick) { if (string.IsNullOrEmpty(trick.Id)) { return(null); } _ctx.Update(trick); await _ctx.SaveChangesAsync(); return(TrickViewModels.Create(trick)); }
public async Task <IActionResult> Update([FromBody] TrickForm trickForm) { var trick = _ctx.Tricks.FirstOrDefault(x => x.Id == trickForm.Id); if (trick == null) { return(NoContent()); } var newTrick = new Trick { Slug = trick.Slug, Name = trick.Name, Version = trick.Version + 1, Description = trickForm.Description, Difficulty = trickForm.Difficulty, Prerequisites = trickForm.Prerequisites .Select(x => new TrickRelationship { PrerequisiteId = x }) .ToList(), Progressions = trickForm.Progressions .Select(x => new TrickRelationship { ProgressionId = x }) .ToList(), TrickCategories = trickForm.Categories .Select(x => new TrickCategory { CategoryId = x }) .ToList(), UserId = UserId, }; _ctx.Add(newTrick); await _ctx.SaveChangesAsync(); _ctx.Add(new ModerationItem { Current = trick.Id, Target = newTrick.Id, Type = ModerationTypes.Trick, // todo validation for reason Reason = trickForm.Reason, UserId = UserId, }); await _ctx.SaveChangesAsync(); // todo redirect to the mod item instead of returning the trick return(Ok(TrickViewModels.Create(newTrick))); }
public async Task <object> Create([FromBody] TrickForm trickForm) { var trick = new Trick { Id = trickForm.Name.Replace(" ", "-").ToLowerInvariant(), Name = trickForm.Name, Description = trickForm.Description, Difficulty = trickForm.Difficulty, Categories = trickForm.Categories.Select(x => new TrickCategory() { CategoryId = x }).ToList() }; _ctx.Add(trick); await _ctx.SaveChangesAsync(); return(TrickViewModels.Create(trick)); }
public object All([FromQuery] FeedQuery feedQuery, int user) { var query = _ctx.ModerationItems.Where(x => !x.Deleted); if (user == 1) { query = query.Where(x => x.UserId == UserId); } var moderationItems = query .Include(x => x.User) .Include(x => x.Reviews) .OrderFeed(feedQuery) .ToList(); var targetMapping = new Dictionary <string, object>(); foreach (var group in moderationItems.GroupBy(x => x.Type)) { var targetIds = group .Select(m => new[] { m.Target, m.Current }) .SelectMany(x => x) .Where(x => x > 0) .ToArray(); if (group.Key == ModerationTypes.Trick) { _ctx.Tricks .Where(t => targetIds.Contains(t.Id)) .ToList() .ForEach(trick => targetMapping[ModerationTypes.Trick + trick.Id] = TrickViewModels.CreateFlat(trick)); } else if (group.Key == ModerationTypes.Category) { _ctx.Categories .Where(c => targetIds.Contains(c.Id)) .ToList() .ForEach(category => targetMapping[ModerationTypes.Category + category.Id] = CategoryViewModels.CreateFlat(category)); } else if (group.Key == ModerationTypes.Difficulty) { _ctx.Difficulties .Where(d => targetIds.Contains(d.Id)) .ToList() .ForEach(difficulty => targetMapping[ModerationTypes.Difficulty + difficulty.Id] = DifficultyViewModels.CreateFlat(difficulty)); } } return(moderationItems.Select(x => new { x.Id, x.Current, x.Target, x.Reason, x.Type, Updated = x.Updated.ToLocalTime().ToString("HH:mm dd/MM/yyyy"), Reviews = x.Reviews.Select(y => y.Status).ToList(), User = UserViewModels.CreateFlat(x.User), CurrentObject = x.Current > 0 ? targetMapping[x.Type + x.Current] : null, TargetObject = x.Target > 0 ? targetMapping[x.Type + x.Target] : null, })); }