コード例 #1
0
ファイル: JobController.cs プロジェクト: ccrcrma/job-portal
        public async Task <IActionResult> EditAsync(int id, JobViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                SetCategories(vm);
                return(View(vm));
            }
            var job = await _context.Jobs
                      .Include(j => j.Company)
                      .FirstOrDefaultAsync(job => job.Id == id);

            if (job == null)
            {
                return(BadRequest());
            }
            var authResult = await _authorizationService.AuthorizeAsync(User, job, new OwnsJobRequirement(job.Company.Name));

            if (authResult.Succeeded)
            {
                job = vm.ToModel(job);
                _context.Entry(job.Category).State = EntityState.Unchanged;
                await _context.SaveChangesAsync();

                return(LocalRedirect("~/").WithSuccess("hurray", "job updated successfully"));
            }
            else if (User.Identity.IsAuthenticated)
            {
                return(new ForbidResult());
            }
            else
            {
                return(new ChallengeResult());
            }
        }
コード例 #2
0
ファイル: JobController.cs プロジェクト: ccrcrma/job-portal
        public async Task <IActionResult> CreateAsync(JobViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                SetCategories(vm);
                return(View(vm));
            }

            var model       = vm.ToModel();
            var companyName = ((ClaimsIdentity)User.Identity).GetSpecificClaim("CompanyName");

            model.Company = await _context.Companies.FirstOrDefaultAsync(c => c.Name == companyName);

            await _context.Jobs.AddAsync(model);

            _context.Entry(model.Category).State = EntityState.Unchanged;
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index", "Dashboard", new { area = "Employer" }).WithSuccess("congrats", "new job added"));
        }