public IActionResult RemoveCompany([FromBody] int id)
        {
            try
            {
                Company company = new Company();
                company = _companiesRepository.GetCompanyById_model(id);

                if (company != null && company.UserId != null)
                {
                    if (company.UserId == IdentityHelper.GetUserId(HttpContext))
                    {
                        _companiesRepository.DeleteCompany(company);
                        return(Ok());
                    }
                    else
                    {
                        return(Unauthorized());
                    }
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }
Exemplo n.º 2
0
        public IActionResult AddJob([FromBody] JobsViewModel jobViewModel)
        {
            try
            {
                Job    job    = Mapper.Map <JobsViewModel, Job>(jobViewModel);
                string UserId = IdentityHelper.GetUserId(HttpContext);

                job.IsApproved   = 0;
                job.Expired      = 0;
                job.UserId       = UserId;
                job.CreationDate = DateTime.Now;
                job.LastUpdate   = DateTime.Now;

                // Ensure the job advertisment runs for atleast 7 days unless it is removed
                if (job.Expiry < DateTime.Now.AddDays(7))
                {
                    return(BadRequest());
                }

                // If a companyId is passed, ensure the company exists and the user has access to it
                if (job.CompanyId.HasValue)
                {
                    Company company = _companiesRepository.GetCompanyById_model(job.CompanyId.Value);
                    if (company == null)
                    {
                        return(BadRequest());
                    }
                    else
                    {
                        if (company.UserId != UserId)
                        {
                            return(Unauthorized());
                        }
                    }
                }

                _jobsRespository.AddJob(job);

                string content = "A new job listing has been added\n" +
                                 "\nID: " + job.JobId +
                                 "\nTitle: " + job.JobTitle +
                                 "\n\nPlease go to https://capstone1.azurewebsites.net/admin to approve this job listing";
                string subject = "New job listing";

                _emailService.SendToAdmins(subject, content);
                return(Ok());
            }
            catch (Exception e)
            {
                return(NotFound());
            }
        }