Beispiel #1
0
        public async Task <IActionResult> Edit(EditViewModel viewModel, string connectionId)
        {
            //Редактирование элемента в базе
            try
            {
                // Проверка номера телефона на кол-во цифр
                Regex regex = new Regex(@"\D+");
                viewModel.Phone = regex.Replace(viewModel.Phone, "");
                if (viewModel.Phone.Length < 7)
                {
                    ModelState.AddModelError("Phone", Messages.phoneMinLengthErrorMessage);
                }

                //Проверка введенных данных
                if (ModelState.IsValid)
                {
                    // Добавление станка при отсутствии в базе
                    var machine = await _machineRepository.GetMachine(viewModel.Machine);

                    if (machine == null)
                    {
                        machine = await _machineRepository.AddMachine(viewModel.Machine, await _machineTypeRepository.AddMachineType("Без группы"));
                    }

                    // Редактируемый элемент
                    var requisition = await _requisitionRepository.GetRequisition(viewModel.Id);

                    requisition.Phone       = viewModel.Phone;
                    requisition.ContactName = viewModel.ContactName;
                    requisition.Location    = viewModel.Location;
                    requisition.MachineId   = machine.Id;
                    requisition.Comment     = viewModel.Comment;

                    await _requisitionRepository.EditRequisition(requisition);

                    await _hub.Clients.Client(connectionId).SendAsync("Success", Messages.successUpdateMessage);

                    return(RedirectToAction("Details", new { id = requisition.Id, ajaxLoad = true }));
                } //if
            }
            catch (DbUpdateException ex) { _message = DbErrorsInterpreter.GetDbUpdateExceptionMessage(ex); }
            catch { _message = Messages.updateErrorMessage; } // try-catch

            viewModel.Machines = await _machineRepository.GetMachines();

            if (_message != null)
            {
                await _hub.Clients.Client(connectionId).SendAsync("Error", _message);
            }

            return(PartialView(viewModel));
        } // Edit
 public HttpResponseMessage Put([FromBody] RequisitionModel RequisitionModel)
 {
     try
     {
         //if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.company_id.ToString()))
         ////if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.company_id.ToString()) && RequisitionModel.RequisitionMasterData.requisition_type != "B2B Requisition")
         //{
         //    var partyFormatter = RequestFormat.JsonFormaterString();
         //    return Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "warning", msg = "Please Select Company" }, partyFormatter);
         //}
         if (string.IsNullOrEmpty(RequisitionModel.RequisitionMasterData.warehouse_from.ToString()))
         {
             var formatter = RequestFormat.JsonFormaterString();
             return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                 output = "error", msg = "Please Select Warehouse !!"
             }, formatter));
         }
         if (RequisitionModel.RequisitionMasterData.edit_count == 3)
         {
             var formatter = RequestFormat.JsonFormaterString();
             return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                 output = "error", msg = "You have already edit three times !!"
             }, formatter));
         }
         else
         {
             requisitionRepository.EditRequisition(RequisitionModel);
             var formatter = RequestFormat.JsonFormaterString();
             return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
                 output = "success", msg = "Requisition Updated successfully"
             }, formatter));
         }
     }
     catch (Exception ex)
     {
         var formatter = RequestFormat.JsonFormaterString();
         return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation {
             output = "error", msg = ex.ToString()
         }, formatter));
     }
 }