예제 #1
0
        public async Task <IActionResult> PutSpot(int id, Spot spot)
        {
            if (id != spot.SpotId)
            {
                return(BadRequest());
            }

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

            if (isAuthorized.Succeeded)
            {
                _spotService.Update(spot);
                try
                {
                    await _spotService.Save();
                }
                catch (BusinessException ex)
                {
                    BadRequest(ex);
                }
            }

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id,
                                               [Bind("MarinaId, LocationId, SpotNumber, Available, MaxWidth, MaxLength, MaxDepth, Price")]
                                               Spot spot)
        {
            try
            {
                var spotDb = await _spotService.GetSingle(id);

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

                if (!isAuthorized.Succeeded)
                {
                    Forbid();
                }

                if (!ModelState.IsValid)
                {
                    ViewData["MarinaId"] = await MarinaList();

                    return(View(spot));
                }

                spotDb.MarinaId   = spot.MarinaId;
                spotDb.LocationId = spot.LocationId;
                spotDb.SpotNumber = spot.SpotNumber;
                spotDb.Available  = spot.Available;
                spotDb.MaxWidth   = spot.MaxWidth;
                spotDb.MaxLength  = spot.MaxDepth;
                spotDb.Price      = spot.Price;

                // If user has chosen a location for the spot by using the
                // Leaflet map
                if (SpotLocationIsSelected())
                {
                    // Either update the location linked to the spot with the
                    // new data Or create related data (Location) for the Spot
                    // and assign the newly created Location to the Spot

                    var location = GetLocationData();
                    if (spotDb.Location == null)
                    {
                        spotDb.Location = location;
                    }
                    else
                    {
                        spotDb.Location.Latitude  = location.Latitude;
                        spotDb.Location.Longitude = location.Longitude;
                    }
                    await _spotService.UpdateSpotLocation(spotDb, spotDb.Location);
                }
                // But if the spot does not have a location now
                else
                {
                    // And if the spot has had a location assigned to it before
                    // but now the user removed it
                    if (spotDb.LocationId != null)
                    {
                        // Delete the spot's location
                        spotDb = await _spotService.DeleteSpotLocation(spotDb);
                    }
                }

                _spotService.Update(spotDb);
                await _spotService.Save();

                return(RedirectToAction(nameof(Index)));
            }
            catch (BusinessException exception)
            {
                Console.WriteLine(exception);
                throw;
            }
        }