public async Task TestDeleteJObAsyncTest() { ApplicationUser user = new ApplicationUser(); Category category = new Category(); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Job>(new ApplicationDbContext(options.Options)); var studentRepo = new EfRepository <StudentJob>(new ApplicationDbContext(options.Options)); var job = new Job { ApplicationUser = user, Salary = 900, Description = "test", }; await repository.AddAsync(job); await repository.SaveChangesAsync(); var studentJob = new StudentJob { Job = job, ApplicationUser = user, }; await studentRepo.AddAsync(studentJob); var jobService = new JobsService(repository, studentRepo); AutoMapperConfig.RegisterMappings(typeof(MyTestJob).Assembly); Assert.Throws <Exception>(() => jobService.DeleteAsync(3516516).GetAwaiter().GetResult()); }
public async Task AddJobAsync(int jobId, string userId) { var user = this.usersRepository.All() .FirstOrDefault(x => x.Id == userId); var job = this.jobsService.JobById(jobId); if (user == null || job == null) { throw new Exception(NotCorrectInput); } var exist = this.studentJobRepository.All().FirstOrDefault(x => x.JobId == jobId && x.ApplicationUserId == userId); if (exist == null) { var studentJob = new StudentJob { ApplicationUserId = userId, ApplicationUser = user, JobId = jobId, Job = job, }; job.Candidates.Add(studentJob); user.StudentJobs.Add(studentJob); await this.jobRepository.SaveChangesAsync(); await this.usersRepository.SaveChangesAsync(); } else { throw new Exception(UserAlreadyApplyOffer); } }