示例#1
0
        public HelpRequestResponse CreateClientHelpRequestResponse(HelpRequestResponseCreateModel vm)
        {
            var helpRequest = _context.HelpRequests
                              .Where(x => x.Id == vm.HelpRequestId)
                              .FirstOrDefault();

            var oldClientHelpRequest = _context.HelpRequestsResponses.Where(x => x.HelpRequest.ClientId == helpRequest.ClientId).ToList();


            oldClientHelpRequest.ForEach(x => x.IsActive = false);

            var newHelpRequest = new HelpRequestResponse
            {
                HelpIncomingDateTimeUtc = vm.ExpirationDateUtc,
                HelpRequestId           = vm.HelpRequestId,
                HelpRequestState        = vm.HelpRequestState,
                IsActive         = true,
                LanguageId       = vm.LanguageId,
                ShortInstruction = vm.ShortInstruction
            };

            _context.HelpRequestsResponses.Add(newHelpRequest);
            _context.SaveChanges();

            return(newHelpRequest);
        }
        public async Task <ActionResult> CreateResponse(HelpRequestResponseCreateModel model)
        {
            async Task <ActionResult> RedisplayResult()
            {
                await _helpRequestFactory.PrepareResponseModel(model);

                return(PartialView("_CreateResponse", model: model));
            }

            if (!ModelState.IsValid)
            {
                return(await RedisplayResult());
            }

            var newDate = model.ExpirationDateUtc.Add(TimeSpan.Parse(model.ExpirationTime));

            model.ExpirationDateUtc = newDate.ToUniversalTime();

            var response = await _httpClientService.SendRequest("api/helprequest/response", HttpMethod.Post, model);

            if (response.IsSuccessStatusCode)
            {
                return(NotificationSuccessResult(Resource.RecordAddSuccess, Url.RouteUrl(HelpRequestsRoutes.Responses, new { model.HelpRequestId })));
            }

            var errorMessage = await _commonService.CheckForValidationErrors(response, Resource.ApplicationErrorText);

            ModelState.AddModelError(string.Empty, errorMessage);

            return(await RedisplayResult());
        }
        public async Task PrepareResponseModel(HelpRequestResponseCreateModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var response = await _httpClientService.GetJsonContent("api/language/dropdowndata");

            model.Languages = new SelectList(
                await response.Content.ReadAsAsync <IEnumerable <LanguageDropDownViewModel> >(),
                nameof(LanguageDropDownViewModel.Id),
                nameof(LanguageDropDownViewModel.Name));

            var templateResponse = await _httpClientService.GetJsonContent("api/maintenance/templates");

            model.InstructionTemplates = await templateResponse.Content.ReadAsAsync <List <DropDownModel> >();
        }
        public async Task <IHttpActionResult> CreateHelpRequestResponse(/*[FromBody]*/ HelpRequestResponseCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Content(HttpStatusCode.BadRequest, GetValidationErrors()));
            }

            var helpRequest = _helpRequestService.GetById(model.HelpRequestId);

            if (helpRequest == null)
            {
                return(NotFound());
            }

            var helpRequestResponse = _helpRequestResponseService.CreateClientHelpRequestResponse(model);

            if (helpRequestResponse == null)
            {
                return(InternalServerError());
            }

            var client = await _clientService.FindById(helpRequest.ClientId);

            var result = MapHelpRequestResponse(helpRequestResponse, client.ClientId);

            bool isSuccess = await _notificationService.SendHelpRequestNotification(client.ClientId, result);

            if (isSuccess)
            {
                return(Ok());
            }
            return(InternalServerError());
        }