Example #1
0
        private AvailableServiceEditViewModel Model_to_viewModel(AvailableService availableService)
        {
            var editModel = new AvailableServiceEditViewModel();

            editModel.ServiceId   = availableService.ServiceId;
            editModel.Provider    = availableService.Provider;
            editModel.Type        = availableService.Type;
            editModel.Description = availableService.Description;

            return(editModel);
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, AvailableServiceEditViewModel editModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(editModel));
            }

            // fetch available services and get ownerID
            var availableService = await _context.AvailableService.SingleOrDefaultAsync(m => m.ServiceId == id);

            if (availableService == null)
            {
                return(NotFound());
            }
            var isAuthorized = await _authorizationService.AuthorizeAsync(User, availableService, ServiceOperations.Update);

            if (!isAuthorized)
            {
                return(new ChallengeResult());
            }

            availableService = ViewModel_to_model(availableService, editModel);

            if (availableService.Status == ServiceStatus.Approved)
            {
                var canApprove = await _authorizationService.AuthorizeAsync(User, availableService, ServiceOperations.Approve);

                if (!canApprove)
                {
                    availableService.Status = ServiceStatus.Submitted;
                }
            }


            try
            {
                _context.Update(availableService);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AvailableServiceExists(availableService.ServiceId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(RedirectToAction("Index"));
        }
Example #3
0
        public async Task <IActionResult> Create(AvailableServiceEditViewModel editModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(editModel));
            }
            var availableService = ViewModel_to_model(new AvailableService(), editModel);

            availableService.OwnerID = _userManager.GetUserId(User);

            var isAuthorized = await _authorizationService.AuthorizeAsync(User, availableService, ServiceOperations.Create);

            if (!isAuthorized)
            {
                return(new ChallengeResult());
            }

            _context.Add(availableService);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Example #4
0
 private AvailableService ViewModel_to_model(AvailableService availableService, AvailableServiceEditViewModel editModel)
 {
     availableService.Provider    = editModel.Provider;
     availableService.Type        = editModel.Type;
     availableService.Description = editModel.Description;
     return(availableService);
 }