Exemplo n.º 1
0
        public async Task <IActionResult> Edit([FromBody] EditedOrderViewModel model)
        {
            if (model.ServiceId == "0" &&
                model.Description == "" &&
                model.ClientAnswers.Count(x => x.Answer == "") == 0 &&
                !model.AnswerIds.Any() ||
                model.SalaryMin == "")
            {
                return(Json("error data"));
            }

            if (model.SalaryMax != null &&
                int.Parse(model.SalaryMax) < int.Parse(model.SalaryMin))
            {
                return(Json("error data"));
            }


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

            var order = await _orderService.FindDetailedAsync(int.Parse(model.Id));

            order.ClientLocation = model.ClientLocation;
            order.ServiceId      = int.Parse(model.ServiceId);
            order.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;
            }
            else
            {
                order.PriceMax = 0;
            }

            var meetingPoints = model.OrderMeetingPoints.Select(x => new OrderMeetingPoint
            {
                OrderId        = order.Id,
                MeetingPointId = int.Parse(x)
            }).ToList();

            if (!order.OrderMeetingPoints.All(x => meetingPoints.Any(y => y.Id == x.Id)))
            {
                order.OrderMeetingPoints = meetingPoints;
            }

            var clientAnswers = model.ClientAnswers
                                .Where(x => x.Answer != "").Select(x => new ClientAnswer
            {
                Text       = x.Answer,
                OrderId    = order.Id,
                QuestionId = x.QuestionId
            }).ToList();


            if (!order.ClientAnswers.All(x => clientAnswers.Any(y => y.Id == x.Id)))
            {
                await _answerService.DeleteInputAnswers(order.ClientAnswers);

                await _answerService.AddInputAnswers(clientAnswers);
            }

            var answers = model.AnswerIds.Select(int.Parse);

            if (!order.AnswerOrders.All(x => answers.Any(y => y == x.Id)))
            {
                await _orderService.UpdateAnswerOrdersAsync(answers, order.Id);
            }

            await _orderService.UpdateAsync(order);

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

            return(Json(order.Id));
        }