public async Task <IActionResult> Edit(ServiceInputModel model)
        {
            if (!this.servicesService.Exists(model.Id))
            {
                return(this.BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(this.RedirectToAction("Error", "Home"));
            }

            EditServiceServiceModel serviceModel = new EditServiceServiceModel
            {
                Id               = model.Id,
                Name             = model.ServiceName,
                Price            = model.Price,
                Description      = model.ServiceDescription,
                IsShownInSubMenu = model.IsShownInSubMenu,

                ServiceTypeId = model.ServiceTypeId,
                VehicleTypeId = model.VehicleTypeId,

                OperatingLocationIds = (model.OperatingLocationIds == null) ? new int[0] : model.OperatingLocationIds,
                DocumentIds          = (model.DocumentIds == null) ? new int[0] : model.DocumentIds,
            };

            await this.servicesService.EditAsync(serviceModel);

            return(this.RedirectToAction("Details", "Services", new { id = serviceModel.Id }));
        }
Exemplo n.º 2
0
        public async Task EditAsync(int id, EditServiceServiceModel serviceModel)
        {
            var service = this.servicesRepository.All().Where(s => s.Id == id).FirstOrDefault();

            service.Name  = serviceModel.Name;
            service.Price = serviceModel.Price;

            await this.servicesRepository.SaveChangesAsync();
        }
        /// <summary>
        /// Edits the <see cref="Service"/> using <see cref="EditServiceServiceModel"/>.
        /// </summary>
        /// <param name="model">Number of modified entities.</param>
        /// <returns>Service model with <c>Id</c>, <c>Name</c>, <c>Description</c>, <c>IsShownInSubMenu</c>, <c>ServiceTypeId</c>, <c>VehicleTypeId</c> and collections of <c>OperatingLocationIds</c> and <c>DocumentIds</c>.</returns>
        public async Task <int> EditAsync(EditServiceServiceModel model)
        {
            Service service = this.dbContext.Services.Find(model.Id);

            service.Name             = model.Name;
            service.Price            = model.Price;
            service.Description      = model.Description;
            service.IsShownInSubMenu = model.IsShownInSubMenu;
            service.VehicleTypeId    = model.VehicleTypeId;
            service.ServiceTypeId    = model.ServiceTypeId;

            //---------------------------------- OPERATING LOCATIONS ----------------------------------
            // First Remove serviceOperatingLocation related entity (Mapping table) for that service
            for (int i = 0; i < service.OperatingLocations.Count; i++)
            {
                this.dbContext.ServiceOperatingLocations.Remove(service.OperatingLocations.ToArray()[i]);
                await this.dbContext.SaveChangesAsync();

                i--;
            }

            // Then Add the new serviceOperatingLocation related entities (Mapping table) for that service with the new OperatingLocations
            foreach (var operatingLocationId in model.OperatingLocationIds)
            {
                await this.dbContext.ServiceOperatingLocations.AddAsync(new ServiceOperatingLocation
                {
                    Service             = service,
                    OperatingLocationId = operatingLocationId,
                });
            }

            //--------------------------------------- DOCUMENTS ---------------------------------------
            // First Remove serviceDocument related entity (Mapping table) for that service
            for (int i = 0; i < service.Documents.Count; i++)
            {
                this.dbContext.ServiceDocuments.Remove(service.Documents.ToArray()[i]);
                await this.dbContext.SaveChangesAsync();

                i--;
            }

            // Then Add the new serviceOperatingLocation related entities (Mapping table) for that service with the new OperatingLocations
            foreach (var documentId in model.DocumentIds)
            {
                await this.dbContext.ServiceDocuments.AddAsync(new ServiceDocument
                {
                    Service    = service,
                    DocumentId = documentId,
                });
            }

            int modifiedEntities = await this.dbContext.SaveChangesAsync();

            return(modifiedEntities);
        }