Пример #1
0
        public async Task <IActionResult> Create([FromBody] CreatedOrderViewModel model)
        {
            if (model.ServiceId == "0" &&
                model.Description == "" &&
                model.ClientAnswers.Count(x => x.Answer == "") == 0 &&
                model.AnswerIds.Count == 0 ||
                model.SalaryMin == "")
            {
                return(Json("error data"));
            }

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var client = await _clientService.FindAsync(userId);

            var order = new Order
            {
                Created        = DateTime.Now,
                State          = OrderStateTypeEnum.InSearchOfSpec,
                ClientLocation = model.ClientLocation,
                Client         = client,
                ServiceId      = int.Parse(model.ServiceId),
                Description    = model.Description
            };

            if (!string.IsNullOrEmpty(model.SalaryMin) &&
                int.TryParse(model.SalaryMin, out var min))
            {
                order.PriceMin = min;
            }
            else
            {
                order.PriceMin = 0;
            }


            if (!string.IsNullOrEmpty(model.SalaryMax) &&
                int.TryParse(model.SalaryMax, out var max))
            {
                order.PriceMax = max;
            }

            var result = await _orderService.InsertAsync(order);

            if (model.OrderMeetingPoints.Any())
            {
                await _orderService.AddMeetingPoints(model.OrderMeetingPoints.Select(x => new OrderMeetingPoint
                {
                    OrderId        = order.Id,
                    MeetingPointId = int.Parse(x)
                }));
            }

            if (model.ClientAnswers.Any(x => x.Answer != ""))
            {
                await _answerService.AddInputAnswers(model.ClientAnswers.Where(x => x.Answer != "").Select(x => new ClientAnswer
                {
                    Text       = x.Answer,
                    OrderId    = order.Id,
                    QuestionId = x.QuestionId
                }));
            }

            if (model.AnswerIds.Any())
            {
                await _answerService.AddAnswersToOrders(model.AnswerIds.Select(int.Parse).ToArray(), order.Id);
            }

            await _hubContext.Clients.All.SendAsync("NewOrder", order.Id);

            return(Json(order.Id));
        }