public IActionResult Create()
        {
            var jobOfferCreate = new JobOfferCreate();

            ViewBag.Companies = _context.Companies.ToList();
            return(View(jobOfferCreate));
        }
        public async Task <ActionResult> Create(JobOfferCreate model)
        {
            ViewBag.Companies = _context.Companies.ToList();
            if (string.IsNullOrWhiteSpace(model.JobTitle))
            {
                ModelState.AddModelError("JobTitle", "Job title cannot be empty");
            }
            if (string.IsNullOrWhiteSpace(model.Location))
            {
                ModelState.AddModelError("Location", "Job location cannot be empty");
            }
            if (string.IsNullOrWhiteSpace(model.ContractType))
            {
                ModelState.AddModelError("ContractType", "Job location cannot be empty");
            }
            if (model.Salary < 0)
            {
                ModelState.AddModelError("Salary", "Salary cannot be negative");
            }
            if (model.CompanyName is null || (!(model.CompanyName is null) && model.CompanyName.Id == 0))
            {
                ModelState.AddModelError("CompanyName", "Company cannot be left empty");
            }
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var newJobOffer = new JobOffer();

            newJobOffer.ContractType    = model.ContractType;
            newJobOffer.Description     = model.Description;
            newJobOffer.JobApplications = new List <JobApplication>();
            newJobOffer.JobTitle        = model.JobTitle;
            newJobOffer.Location        = model.Location;
            newJobOffer.CompanyName     = _context.Companies.FirstOrDefault(x => x.Id == model.CompanyName.Id);
            newJobOffer.CompanyNameId   = model.CompanyName.Id;

            await _context.JobOffers.AddAsync(newJobOffer);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
示例#3
0
        public async Task <IActionResult> Post([FromBody] JobOfferCreate offer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newOffer = new JobOffer()
            {
                CompanyId   = offer.CompanyId,
                Description = offer.Description,
                JobTitle    = offer.JobTitle,
                Location    = offer.Location,
                SalaryFrom  = offer.SalaryFrom,
                SalaryTo    = offer.SalaryTo,
                ValidUntil  = offer.ValidUntil,
                Created     = DateTime.Now
            };

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

            return(CreatedAtAction("Offers", new { id = newOffer.Id }, offer));
        }