Пример #1
0
        public IHttpActionResult Save([FromBody] TripApplicationDto dto)
        {
            if (!ModelState.IsValid)
            {
                var errors    = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message);
                var dataError = new ApiErrorDto("Save Trip Application Data Invalid", new InvalidOperationException("Invalid Save Data" + errors));
                throw new HttpResponseException(dataError.HttpResponseMessage);
            }

            TripApplicationResponseDto response;

            try
            {
                var participantPledgeInfo = _tripService.CreateTripParticipant(dto.ContactId, dto.PledgeCampaignId);
                var message = _messageFactory.CreateMessage(dto);
                _eventQueue.Send(message, MessageQueueTransactionType.None);
                response = new TripApplicationResponseDto
                {
                    Message       = "Queued event for asynchronous processing",
                    DepositAmount = participantPledgeInfo.Deposit,
                    DonorId       = participantPledgeInfo.DonorId,
                    ProgramId     = participantPledgeInfo.ProgramId,
                    ProgramName   = participantPledgeInfo.ProgramName
                };

                new Task(() => { _tripService.SendTripIsFullMessage(dto.PledgeCampaignId); }).Start();
            }
            catch (TripFullException e)
            {
                var json    = JsonConvert.SerializeObject(e.Message, Formatting.None);
                var message = new HttpResponseMessage(HttpStatusCode.Conflict);
                message.Content = new StringContent(json);
                throw new HttpResponseException(message);
            }
            catch (Exception e)
            {
                const string msg         = "Unexpected error processing Trip Application Save";
                var          responseDto = new TripApplicationResponseDto()
                {
                    Exception = new ApplicationException(msg, e),
                    Message   = msg
                };
                return(RestHttpActionResult <TripApplicationResponseDto> .ServerError(responseDto));
            }
            return((IHttpActionResult)RestHttpActionResult <TripApplicationResponseDto> .Ok(response));
        }