예제 #1
0
        public IActionResult Create([FromBody] RsvpDto rsvpDto)
        {
            // map dto to entity
            var rsvp = _mapper.Map <Rsvp>(rsvpDto);

            try
            {
                // save
                _rsvpService.Create(rsvp);
                return(Ok());
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
예제 #2
0
        public IActionResult Rsvp([FromBody] RsvpDto rsvpDto)
        {
            try
            {
                this.logger.LogDebug("Rsvp");

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

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

                // Verify meet exists
                bool meetExists = this.dbContext.Meets.Any(m => m.MeetId == rsvpDto.MeetId);

                if (!meetExists)
                {
                    return(BadRequest(new ErrorResponse("Shackmeet does not exist.")));
                }

                var rsvp = this.dbContext.Rsvps.SingleOrDefault(r => r.MeetId == rsvpDto.MeetId && r.Username == currentUsername);

                if (rsvp != null)
                {
                    // Existing RSVP
                    this.dbContext.Rsvps.Attach(rsvp);
                }
                else
                {
                    // New RSVP
                    rsvp = new Rsvp
                    {
                        MeetId   = rsvpDto.MeetId,
                        Username = currentUsername
                    };

                    this.dbContext.Add(rsvp);
                }

                rsvp.RsvpType     = rsvpDto.RsvpType;
                rsvp.NumAttendees = rsvpDto.NumAttendees;

                // Validate fields
                var validator        = new RsvpValidator();
                var validationResult = validator.Validate(rsvp);

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

                this.dbContext.SaveChanges();

                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()));
            }
        }