Example #1
0
        public IActionResult CreateBoxer([FromBody] BoxerDto data)
        {
            // We can post the data of the entity to be created in 2 ways. The first of these is "[FromBody]", which we will prefer when we send the data from the body of the request. In the second "[FromUri]" approach, it is necessary to send a value to the propery of the asset we want to post from the browser address bar. It is unnecessary as it is a laborious task and we will use any of the tools like (Postman-Fiddler-Swashbuckle) as the interface in this project while testing the api.

            if (ModelState.IsValid)
            {
                if (data == null)
                {
                    return(BadRequest(data));                 // Return Status Code 400
                }
                if (_repository.IsBoxerExsist(data.FullName)) // We checked if there is any boxer with the same name in the database. If there is, it will return True and the if block will be activated.
                {
                    ModelState.AddModelError("", "The boxer already exsist..!");
                    return(StatusCode(404, ModelState));
                }

                var boxerObject = _mapper.Map <Boxer>(data);
                // Since we used assets in the repository in previous projects, we could not use Data Transfer Objects in our methods in the repository. We have solved this big problem with AutoMapper.

                if (!_repository.Create(boxerObject)) // In this project, we have defined our methods to carry out CRUD operations as "boolean", mainly of return types. Here, if the insertion fails, we go through the scenario.

                {
                    ModelState.AddModelError("", $"Something went wrong when saving the record {data.FullName}");
                    return(StatusCode(500, ModelState));
                }

                // return CreatedAtRoute ("GetBoxers", boxerObject); // GetBoxers action git, go with the data of the boxer created while going
                // In the following redirect, it takes the id parameter from the two "GetBoxers" methods and only requests the route to be the method that brings the boxer according to the parameter it gets. It will also carry the data of the boxer created like the method above while going.

                return(CreatedAtRoute("GetBoxers", new { id = boxerObject.Id }, boxerObject));
            }

            return(BadRequest());
        }
Example #2
0
 public async Task Update(int id, [FromBody] BoxerDto boxer)
 {
     boxer.Id = id;
     var request = new UpdateBoxerRequest
     {
         Boxer = boxer
     };
     await _mediator.ExecuteAsync(request).ConfigureAwait(false);
 }
Example #3
0
        public async Task <HttpResponseMessage> Create([FromBody] BoxerDto boxer)
        {
            var request = new CreateBoxerRequest
            {
                Boxer = boxer
            };
            await _mediator.ExecuteAsync(request).ConfigureAwait(false);

            return(Request.CreateResponse(HttpStatusCode.Created));
        }
Example #4
0
        public IActionResult UpdateBoxer(int id, [FromBody] BoxerDto data)
        {
            if (data == null)
            {
                return(BadRequest(data));
            }

            var boxerObject = _mapper.Map <Boxer>(data);

            if (!_repository.Update(boxerObject))
            {
                ModelState.AddModelError("", $"Something went wrong when editing record {data.FullName}");
                return(StatusCode(500, data));
            }

            return(Ok());
        }