public async Task <IActionResult> Edit(int id) { var order = await _orderService.FindDetailedAsync(id); var subCategory = await _categoryService .GetSubCategoryAsync(order.Service.SubCategoryId); var model = new EditedOrderViewModel(); model.Id = order.Id.ToString(); model.ClientLocation = order.ClientLocation; model.Description = order.Description; model.MeasurmentId = order.MeasurementId.ToString(); model.SalaryMin = order.PriceMin.ToString(); model.SalaryMax = order.PriceMax.ToString(); model.OrderMeetingPoints = order.OrderMeetingPoints .Select(x => x.MeetingPointId.ToString()).ToList(); model.ClientAnswers = order.ClientAnswers .Select(x => new ClientInputAnswer { QuestionId = x.QuestionId, Answer = x.Text }).ToList(); model.CategoryId = subCategory.CategoryId.ToString(); model.SubCategoryId = subCategory.Id.ToString(); model.ServiceId = order.ServiceId.ToString(); model.AnswerIds = order.AnswerOrders .Select(x => x.AnswerId.ToString()).ToList(); var isRu = CultureInfo.CurrentCulture.Name == "ru-RU"; var categories = new List <Category>(); var measurments = await _categoryService.FindAllMeasurements(); categories.Add(new Category { Id = 0, DescriptionRU = "Выберите категорию", DescriptionAZ = "Kateqoriya seçin" }); categories.AddRange(await _categoryService.GetAllCategories()); ViewBag.Categories = new SelectList(categories, "Id", isRu ? "DescriptionRU" : "DescriptionAZ"); ViewBag.SubCategories = new SelectList(new[] { new { Id = 0, Text = isRu ? "Выберите подкатегорию" : "Alt kateqoriyanı seçin" } }, "Id", "Text"); ViewBag.Services = new SelectList(new[] { new { Id = 0, Text = isRu ? "Выберите услугу" : "Xidməti seçin" } }, "Id", "Text"); ViewBag.Measurments = new SelectList(measurments, "Id", isRu ? "TextRU" : "TextAZ"); return(View(model)); }
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)); }