public ActionResult Save(int?id)
        {
            var services   = _context.Services.Include(d => d.ServiceType).ToList();
            var instrument = new Instrument();

            if (id != null)
            {
                instrument = _context.Instruments.Find(id);

                var instrumentServices = _context.InstrumentServices.Where(d => d.InstrumentId == instrument.InstrumentId);

                foreach (var service in services)
                {
                    if (instrumentServices.Any(d => d.ServiceId == service.ServiceId))
                    {
                        service.IsChecked = true;
                    }
                }
            }

            var model = new CreateInstrumentViewModel()
            {
                Services   = services,
                Instrument = instrument
            };

            return(View(model));
        }
Beispiel #2
0
 public Instrument(CreateInstrumentViewModel createInstrumentViewModel)
 {
     MakeId         = createInstrumentViewModel.SelectedMakeId;
     InstrumentType = createInstrumentViewModel.SelectedInstrumentType;
     ProgramId      = createInstrumentViewModel.SelectedProgramId;
     //add user later
     SerialNumber = createInstrumentViewModel.SerialNumber;
     Notes        = createInstrumentViewModel.Notes;
 }
        public async Task <IActionResult> Create(CreateInstrumentViewModel createInstrumentViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(createInstrumentViewModel));
            }

            var instrument = new Instrument(createInstrumentViewModel);

            var instrumentId = await _mediator.Send(CreateInstrument.With(instrument));

            if (instrumentId == 0)
            {
                return(View("~/Shared/Views/Shared/Error.cshtml"));
            }

            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult Save(CreateInstrumentViewModel model)
        {
            var instrumentServices = _context.InstrumentServices
                                     .Where(d => d.InstrumentId == model.Instrument.InstrumentId).ToList();

            //var services = _context.Services.Include(d => d.ServiceType).ToList();

            if (model.Instrument.InstrumentId == 0)
            {
                _context.Instruments.Add(model.Instrument);
                _context.SaveChanges();
            }
            else
            {
                var instrument = _context.Instruments.Find(model.Instrument.InstrumentId);
                instrument.Name     = model.Instrument.Name;
                instrument.ImageUrl = model.Instrument.ImageUrl;
            }

            foreach (var service in model.Services)
            {
                if (service.IsChecked && instrumentServices.All(d => d.ServiceId != service.ServiceId))
                {
                    _context.InstrumentServices.Add(new InstrumentServices()
                    {
                        ServiceId    = service.ServiceId,
                        InstrumentId = model.Instrument.InstrumentId
                    });
                }
                else if (!service.IsChecked && instrumentServices.Any(d => d.ServiceId == service.ServiceId))
                {
                    _context.InstrumentServices.Remove(instrumentServices
                                                       .First(d => d.ServiceId == service.ServiceId && d.InstrumentId == model.Instrument.InstrumentId));
                }
                else if (service.IsChecked && instrumentServices.Any(d => d.ServiceId == service.ServiceId))
                {
                    service.IsChecked = !service.IsChecked;
                }
            }

            _context.SaveChanges();

            return(RedirectToAction("Index"));
        }