예제 #1
0
        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 <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)));
        }
예제 #3
0
        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,
                TrickCategories = trickForm.Categories.Select(x => new TrickCategory {
                    CategoryId = x
                }).ToList()
            };

            _ctx.Add(trick);
            await _ctx.SaveChangesAsync();

            return(TrickViewModels.Default.Compile().Invoke(trick));
        }