Ejemplo n.º 1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUserId = userAccessor.GetCurrentUserId();

                var currentUser = await userManager.FindByIdAsync(currentUserId);

                if (currentUser == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }
                if (await userManager.IsInRoleAsync(currentUser, RoleNames.User))
                {
                    throw new RestException(HttpStatusCode.Forbidden, new { Forbidden = "Insufficient role privileges. Permission Denied." });
                }

                Trail trailToEdit = await trailsService.FindByIdAsync(request.Id);

                trailToEdit.Name                 = request.Name;
                trailToEdit.Description          = request.Description;
                trailToEdit.Length               = request.Length;
                trailToEdit.Difficulty           = request.Difficulty;
                trailToEdit.Type                 = request.Type;
                trailToEdit.Traffick             = request.Traffick;
                trailToEdit.Attractions          = request.Attractions;
                trailToEdit.Suitabilities        = request.Suitabilities;
                trailToEdit.Image                = request.Image ?? trailToEdit.Image;
                trailToEdit.Trailhead.Country    = request.Trailhead.Country;
                trailToEdit.Trailhead.Street     = request.Trailhead.Country == "United States" ? request.Trailhead.Street : null;
                trailToEdit.Trailhead.Street2    = request.Trailhead.Country == "United States" ? request.Trailhead.Street2 ?? trailToEdit.Trailhead.Street2 : null;
                trailToEdit.Trailhead.City       = request.Trailhead.Country == "United States" ? request.Trailhead.City : null;
                trailToEdit.Trailhead.County     = request.Trailhead.Country == "United States" ? request.Trailhead.County : null;
                trailToEdit.Trailhead.State      = request.Trailhead.Country == "United States" ? request.Trailhead.State : null;
                trailToEdit.Trailhead.PostalCode = request.Trailhead.Country == "United States" ? request.Trailhead.PostalCode : null;
                trailToEdit.Trailhead.Latitude   = request.Trailhead.Latitude;
                trailToEdit.Trailhead.Longitude  = request.Trailhead.Longitude;
                trailToEdit.Trailhead.Altitude   = request.Trailhead.Altitude ?? trailToEdit.Trailhead.Altitude;
                trailToEdit.Trailhead.InternationalLocation.Address = request.Trailhead.Country != "United States" ? request.Trailhead.InternationalLocation.Address : null;
                trailToEdit.DateModified = DateTimeOffset.UtcNow;
                trailToEdit.UpdatedBy    = currentUser.Id;

                var succeeded = await repository.UpdateAsync(trailToEdit);

                if (succeeded)
                {
                    return(Unit.Value);
                }

                throw new RestException(HttpStatusCode.BadRequest, new { EditTrail = "Problem updating trail." });
            }
 public async Task <Trail> Handle(Query request, CancellationToken cancellationToken)
 {
     // bool isValid = Guid.TryParse(request.Id, out Guid trailId);
     // if (!isValid) throw new ArgumentException("Invalid Trail Id Provided");
     return(await trailsService.FindByIdAsync(request.Id));
 }