public async Task <bool> RemoveAsync(int id) { ProExp entity = await _dbContext.ProExps.FirstOrDefaultAsync(x => x.Id == id); if (entity != null) { var profileTechnologies = await _dbContext.ProExps .Where(x => x.Id == id) .SelectMany(x => x.Technologies) .Join(_dbContext.ProfileTechnologies, techFromProExp => techFromProExp.Id, profileTech => profileTech.TechnologyId, (proExpTech, profileTech) => new ProfileTechnology() { ProfileId = profileTech.ProfileId, TechnologyId = profileTech.TechnologyId }) .ToListAsync(); foreach (var profileTechnology in profileTechnologies) { _dbContext.ProfileTechnologies.Remove(profileTechnology); } _dbContext.ProExps.Remove(entity); } await _dbContext.SaveChangesAsync(); return(true); }
public async Task <bool> RemoveAsync(int id) { using (ApplicationContext context = new ApplicationContext()) { ProExp entity = await context.ProExps.FirstOrDefaultAsync(x => x.Id == id); if (entity != null) { context.ProExps.Remove(entity); } await context.SaveChangesAsync(); return(true); } }
public async Task <int> CreateAsync(ProExpModel proExpModel) { ProExp entity = new ProExp { ProfileId = proExpModel.ProfileId, FromDate = proExpModel.FromDate, ToDate = proExpModel.ToDate, City = await GetOrCreateCityAsync(proExpModel.CityName), Company = await GetOrCreateCompanyAsync(proExpModel.CompanyName), Technologies = await GetOrCreateTechnoloyAsync(proExpModel.TechnologyModels, proExpModel.ProfileId), ExperienceDescriptions = await CreateOrUpdateExpDescAsync(proExpModel.ExperienceDescriptionModel) }; _dbContext.ProExps.Add(entity); await _dbContext.SaveChangesAsync(); return(entity.Id); }
// Conservation d'un seul service qui s'occupe de l'expérience professionnel et qui ajoute des données dans différentes tables. // TODO voir pour refacto ces deux fonctions assez similaires public async Task <int> CreateAsync(ProExpModel proExpModel) { ProExp entity = new ProExp { ProfileId = proExpModel.ProfileId, FromDate = proExpModel.FromDate, ToDate = proExpModel.ToDate }; using (ApplicationContext context = new ApplicationContext()) { // CITY PART var city = await context.Cities .FirstOrDefaultAsync(x => x.Name == proExpModel.CityName); if (city == null) { city = new City { Name = proExpModel.CityName }; } entity.City = city; // COMPANY PART var company = await context.Companies .FirstOrDefaultAsync(x => x.Name == proExpModel.CompanyName); if (company == null) { company = new Company { Name = proExpModel.CompanyName }; } entity.Company = company; // TECHNOLOGY PART if (proExpModel.TechnologyModels != null && proExpModel.TechnologyModels.Count > 0) { foreach (var technologyModel in proExpModel.TechnologyModels) { Technology technology = await context.Technologies.FirstOrDefaultAsync(x => x.Title == technologyModel.Title); if (technology == null) { technology = new Technology { Title = technologyModel.Title }; } entity.Technologies.Add(technology); context.ProfileTechnologies.AddOrUpdate(new ProfileTechnology { ProfileId = proExpModel.ProfileId, TechLevelId = technologyModel.TechLevelId, Technology = technology }); } } context.ProExps.Add(entity); await context.SaveChangesAsync(); } // Description Part with other Service var description = proExpModel.ExperienceDescriptionModel; if (description != null) { description.ProExpId = entity.Id; await _expDescriptionService.CreateAsync(description); } return(entity.Id); }