Esempio n. 1
0
        public AddInstrumentBindingModel GetAllBrandsAndInstrumentTypes()
        {
            var brands = this.DbContext
                         .Brands
                         .Select(this.Mapper.Map <BrandConciseViewModel>)
                         .ToList();

            var instrumentTypes = this.DbContext
                                  .InstrumentTypes
                                  .Select(this.Mapper.Map <InstrumentTypeViewModel>)
                                  .ToList();

            var brandsQuery = brands.Select(b => new SelectListItem()
            {
                Text = b.BrandName, Value = b.Id.ToString()
            });
            var instrumentTypesQuery = instrumentTypes.Select(b => new SelectListItem()
            {
                Text = b.TypeName, Value = b.Id.ToString()
            });

            var model = new AddInstrumentBindingModel()
            {
                AllBrands          = brandsQuery.ToList(),
                AllInstrumentTypes = instrumentTypesQuery.ToList()
            };

            return(model);
        }
        public async Task <IActionResult> AddInstrument(AddInstrumentBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                SetErrorMessage(CommonConstants.DangerMessage);

                return(this.AddInstrument());
            }

            int generatedId = await this.instrumentService.AddInstrumentAsync(model);

            if (generatedId < 1)
            {
                SetErrorMessage(CommonConstants.DuplicateMessage);

                return(this.AddInstrument());
            }

            SetSuccessMessage(string.Format(CommonConstants.SuccessMessage, CommonConstants.InstrumentSuffix));

            return(Redirect(string.Format(RedirectConstants.AdministratorAreaInstrumentDetailsPage, generatedId)));
        }
Esempio n. 3
0
        public async Task <int> AddInstrumentAsync(AddInstrumentBindingModel model)
        {
            var checkForDuplicate = this.DbContext
                                    .Instruments
                                    .FirstOrDefault(x => x.ModelName == model.ModelName);

            if (checkForDuplicate != null)
            {
                return(ErrorId);
            }

            var instrument = this.Mapper.Map <Instrument>(model);

            var instrumentType = this.DbContext
                                 .InstrumentTypes
                                 .FirstOrDefault(x => x.Id == model.InstrumentTypeId);

            var brand = this.DbContext
                        .Brands
                        .FirstOrDefault(x => x.Id == model.BrandId);

            instrument.InstrumentTypeId = instrumentType.Id;
            instrument.InstrumentType   = instrumentType;

            instrument.BrandId = brand.Id;
            instrument.Brand   = brand;

            if (instrument.HighLightVideoURL.Contains(CommonConstants.OriginalVideoUrlPart))
            {
                instrument.HighLightVideoURL = ModifyVideoURL_Embeded.ModifyEmbed(instrument.HighLightVideoURL);
            }

            await this.DbContext.Instruments.AddAsync(instrument);

            await this.DbContext.SaveChangesAsync();

            return(instrument.Id);
        }