示例#1
0
        public async Task <IActionResult> UpdateStaged([FromBody] UpdateDifficultyForm form)
        {
            var difficulty = _ctx.Difficulties
                             .FirstOrDefault(x => x.Id == form.Id);

            if (difficulty == null)
            {
                return(NoContent());
            }
            if (difficulty.UserId != UserId)
            {
                return(BadRequest("Can't edit this difficulty."));
            }

            difficulty.Description = form.Description;

            await _ctx.SaveChangesAsync();

            return(Ok());
        }
示例#2
0
        public async Task <IActionResult> Update([FromBody] UpdateDifficultyForm form)
        {
            var difficulty = _ctx.Difficulties.FirstOrDefault(x => x.Id == form.Id);

            if (difficulty == null)
            {
                return(NoContent());
            }

            var newDifficulty = new Difficulty
            {
                Slug        = form.Name.Replace(" ", "-").ToLowerInvariant(),
                Name        = form.Name,
                Description = form.Description,
                UserId      = UserId,
            };

            newDifficulty.Version = difficulty.Slug.Equals(newDifficulty.Slug, StringComparison.InvariantCultureIgnoreCase)
                ? difficulty.Version + 1
                : 1;

            _ctx.Add(newDifficulty);

            await _ctx.SaveChangesAsync();

            _ctx.Add(new ModerationItem
            {
                Current = difficulty.Id,
                Target  = newDifficulty.Id,
                UserId  = UserId,
                Type    = ModerationTypes.Difficulty,
            });
            await _ctx.SaveChangesAsync();

            return(Ok());
        }