Beispiel #1
0
        /// <summary>
        /// Attempts to update a spell. This is a PUT because a PATCH is too much
        /// work for such a small model.
        ///
        /// ROUTE
        /// `api/spell/{id}`
        ///
        /// REQUEST BODY
        /// {
        ///     "name": `string`,
        ///     "level": `int`,
        ///     "school": `string`,
        ///     "castingTime": `string`,
        ///     "range": `string`,
        ///     "verbal": `bool`,
        ///     "somatic": `bool`,
        ///     "materials": `string`,
        ///     "duration": `string`,
        ///     "ritual": `bool`,
        ///     "description": `string`
        /// }
        ///
        /// RESPONSE BODY
        /// {
        ///     "success": `bool`,
        ///     "message": `string`,
        ///     "spell": {
        ///         "id": `int`,
        ///         "name": `string`,
        ///         "level": `int`,
        ///         "school": `string`,
        ///         "castingTime": `string`,
        ///         "range": `string`,
        ///         "verbal": `bool`,
        ///         "somatic": `bool`,
        ///         "materials": `string`,
        ///         "duration": `string`,
        ///         "ritual": `bool`,
        ///         "description": `string`
        ///     }
        /// }
        /// </summary>
        /// <param name="id">The ID of the spell.</param>
        /// <returns>The response body.</returns>
        public async Task <SpellUpdateResponse> Put(int id, SpellUpdateRequest request)
        {
            var response = new SpellUpdateResponse()
            {
                Success = false
            };

            if (ModelState.IsValid)
            {
                SchoolOfMagic school;
                if (Enum.TryParse(request.School, out school))
                {
                    var spell = new Spell()
                    {
                        Id          = id,
                        Name        = request.Name,
                        Level       = request.Level.Value,
                        School      = school,
                        CastingTime = request.CastingTime,
                        Range       = request.Range,
                        Verbal      = request.Verbal.Value,
                        Somatic     = request.Somatic.Value,
                        Materials   = request.Materials,
                        Duration    = request.Duration,
                        Ritual      = request.Ritual.Value,
                        Description = request.Description
                    };

                    if (await _spellRepo.UpdateAsync(spell))
                    {
                        response.Success = true;
                        response.Spell   = spell.GetInfo();
                    }
                }
            }
            return(response);
        }