Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("MarinaId, SpotNumber, Available, MaxWidth, MaxLength, MaxDepth, Price")] Spot spot)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewData["MarinaId"] = await MarinaList();

                    return(View(spot));
                }

                // If user has chosen a location for the spot by using the
                // Leaflet map
                if (SpotLocationIsSelected())
                {
                    // Create related data (Location) for the Spot and assign
                    // the newly created Location to the Spot
                    var location = GetLocationData();
                    await _spotService.CreateWithLocation(spot, location);
                }
                else
                {
                    await _spotService.Create(spot);
                }

                await _spotService.Save();

                return(RedirectToAction(nameof(Index)));
            }
            catch (BusinessException exception)
            {
                ModelState.TryAddModelError(exception.Key, exception.Message);
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Spot> > PostSpot(Spot spot)
        {
            if (spot.SpotId != 0 || spot.Location?.LocationId != 0)
            {
                BadRequest("Do not set the ID");
            }

            if (spot.MarinaId is not null)
            {
                var marina = await _marinaService.GetSingle(spot.MarinaId);

                var isAuthorized = await _authorizationService.AuthorizeAsync(User, marina, Operation.Update);

                if (!isAuthorized.Succeeded)
                {
                    return(StatusCode(403));
                }
            }
            await _spotService.Create(spot);

            await _spotService.Save();

            return(CreatedAtAction("GetSpot", new { id = spot.SpotId }, spot));
        }