コード例 #1
0
        public async Task <ActionResult <OwnerViewModel> > Post(OwnerViewModel createOwner)
        {
            OwnerModel owner     = _mapper.MapObjectTo <OwnerModel>(createOwner);
            bool       isCreated = await _ownerService.CreateOwnerAsync(owner);

            return(isCreated ? Created(Url.Action(nameof(GetOwnerById), new { id = owner.Id }), _mapper.MapObjectTo <OwnerViewModel>(owner)) as ActionResult : BadRequest() as ActionResult);
        }
コード例 #2
0
        public async Task <IActionResult> CreateOwner([FromBody] OwnerForCreationDto owner)
        {
            try
            {
                if (owner == null)
                {
                    _logger.LogError("Owner object sent from client is null.");
                    return(BadRequest("Owner object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid owner object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var createdOwner = await _ownerService.CreateOwnerAsync(owner);

                return(CreatedAtRoute("OwnerById", new { id = createdOwner.Id }, createdOwner));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateOwner action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
コード例 #3
0
        public async Task <IActionResult> Post([FromBody] CreateOwnerRequest ownerRequest)
        {
            var owner = new Owner
            {
                Name        = ownerRequest.Name,
                DateOfBirth = ownerRequest.DateOfBirth,
                Address     = ownerRequest.Address
            };

            await _ownerService.CreateOwnerAsync(owner);

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent().ToString()}";
            var locationUrl = baseUrl + "/" + ApiRoutes.Owners.Get.Replace("{ownerId}", owner.Id.ToString());

            var response = new OwnerResponse {
                Id = owner.Id
            };

            return(Created(locationUrl, response));
        }