public async Task <IActionResult> Create([Bind("BoatId,Name,RegistrationNo,Width,Length,Depth,Type,BoatOwnerId")] Boat boat)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // Check whether user is allowed to create a boat
                    var isAuthorized = await _authorizationService.AuthorizeAsync(User, boat, Operation.Create);

                    // If he is authorized for that
                    if (isAuthorized.Succeeded)
                    {
                        // Create a boat
                        await _boatService.Create(boat);

                        // Persist changes to the database
                        await _boatService.Save();

                        // Return to the Index view of all boats
                        return(RedirectToAction(nameof(Index)));
                    }

                    // Forbid the post if user is not authorized for that
                    return(Forbid());
                }
                catch (BusinessException)
                {
                    return(View("Error"));
                }
            }

            return(View(boat));
        }
示例#2
0
        public async Task <IActionResult> Create([FromForm] IFormFile file, [FromForm] string jsonString)
        {
            var userId        = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;
            var createBoatDto = JsonConvert.DeserializeObject <CreateBoatDto>(jsonString);

            createBoatDto.UserId = int.Parse(userId);

            if (!(file is null))
            {
                var imageUrl = await GetUploadedFileUrl(file);

                createBoatDto.ImageUrl = imageUrl;
            }

            await _boatService.Create(createBoatDto);

            return(Ok());
        }