Ejemplo n.º 1
0
        public IActionResult UpdateShackmeet([FromBody] MeetDto meetDto)
        {
            try
            {
                this.logger.LogDebug("UpdateShackmeet");

                // Verify input exists
                if (meetDto == null)
                {
                    return(BadRequest(new BadInputResponse()));
                }

                // Load meet with related RSVPs and users (for use with notifications)
                var meet = this.dbContext
                           .Meets
                           .Where(m => m.MeetId == meetDto.MeetId)
                           .Include(m => m.Rsvps)
                           .ThenInclude(r => r.User)
                           .SingleOrDefault(m => m.MeetId == meetDto.MeetId);

                // Verify meet exists
                if (meet == null)
                {
                    return(BadRequest(new ValidationErrorResponse("meetId", "Shackmeet does not exist.")));
                }

                // Use the username of the authenticated user so they can only mess with their own shackmeets.
                var currentUsername = this.User.FindFirst(ClaimTypes.Name).Value;

                if (meet.OrganizerUsername != currentUsername)
                {
                    return(BadRequest(new ErrorResponse("Only the organizer can update a shackmeet.")));
                }

                // Get additional address info (and verify address)
                var addressInfo = this.googleMapsService.GetAddressInfo(meetDto.LocationAddress);

                if (!addressInfo.IsValid)
                {
                    return(BadRequest(new ValidationErrorResponse("locationAddress", "Address does not correspond to a valid geocode location.")));
                }

                this.dbContext.Meets.Attach(meet);

                // Update meet
                meet.Name                 = meetDto.Name;
                meet.Description          = meetDto.Description;
                meet.OrganizerUsername    = meetDto.OrganizerUsername;
                meet.EventDate            = meetDto.EventDate;
                meet.LocationName         = meetDto.LocationName;
                meet.LocationAddress      = meetDto.LocationAddress;
                meet.LocationState        = addressInfo.State;
                meet.LocationCountry      = addressInfo.Country;
                meet.LocationLatitude     = addressInfo.Latitude;
                meet.LocationLongitude    = addressInfo.Longitude;
                meet.WillPostAnnouncement = meetDto.WillPostAnnouncement;
                meet.IsCancelled          = false;

                // Validate fields
                var validator        = new MeetValidator();
                var validationResult = validator.Validate(meet);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(new ValidationErrorResponse(validationResult.Messages)));
                }

                this.dbContext.SaveChanges();

                // Send update notifications

                return(Ok(new SuccessResponse()));
            }
            catch (Exception e)
            {
                // Log error
                this.logger.LogError("Message: {0}" + Environment.NewLine + "{1}", e.Message, e.StackTrace);

                return(BadRequest(new CriticalErrorResponse()));
            }
        }
Ejemplo n.º 2
0
        public IActionResult CreateShackmeet([FromBody] MeetDto meetDto)
        {
            try
            {
                this.logger.LogDebug("CreateShackmeet");

                // Verify input exists
                if (meetDto == null)
                {
                    return(BadRequest(new BadInputResponse()));
                }

                // PRL
                int numRecentMeets = this.dbContext.Meets.Count(m => m.OrganizerUsername == meetDto.OrganizerUsername && m.TimestampCreate >= DateTime.Now.AddMinutes(-5));

                if (numRecentMeets >= 2)
                {
                    return(BadRequest(new ErrorResponse("Slow your rolls.")));
                }

                // Get additional address info (and verify address)
                var addressInfo = this.googleMapsService.GetAddressInfo(meetDto.LocationAddress);

                if (!addressInfo.IsValid)
                {
                    return(BadRequest(new ValidationErrorResponse("locationAddress", "Address does not correspond to a valid geocode location.")));
                }

                // Create new meet
                var meet = new Meet
                {
                    Name                 = meetDto.Name,
                    Description          = meetDto.Description,
                    OrganizerUsername    = meetDto.OrganizerUsername,
                    EventDate            = meetDto.EventDate,
                    LocationName         = meetDto.LocationName,
                    LocationAddress      = meetDto.LocationAddress,
                    LocationState        = addressInfo.State,
                    LocationCountry      = addressInfo.Country,
                    LocationLatitude     = addressInfo.Latitude,
                    LocationLongitude    = addressInfo.Longitude,
                    WillPostAnnouncement = meetDto.WillPostAnnouncement,
                    IsCancelled          = false
                };

                // Validate fields
                var validator        = new MeetValidator();
                var validationResult = validator.Validate(meet);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(new ValidationErrorResponse(validationResult.Messages)));
                }

                this.dbContext.Meets.Add(meet);
                this.dbContext.SaveChanges();

                // Send new shackmeet notifications

                return(Ok(new InsertSuccessResponse(meet.MeetId)));
            }
            catch (Exception e)
            {
                // Log error
                this.logger.LogError("Message: {0}" + Environment.NewLine + "{1}", e.Message, e.StackTrace);

                return(BadRequest(new CriticalErrorResponse()));
            }
        }