public void addCompany(string companyS, string[] countries) { var company = new Company { companyName = companyS, }; for (int i = 0; i < countries.Length; i++) { var country = new Country { countryName = countries[i] }; CompanyCountry cc = new CompanyCountry { companyId = company.companyId, company = company, countryId = country.countryId, country = country }; companyCountries.Add(cc); } }
public async Task <IHttpActionResult> UpdateMyCompanyProfile(CompanyProfileDTO companyProfileDTO) { string userName = User.Identity.Name; User authenticatedUser = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (authenticatedUser == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } User user = db.Users.Find(companyProfileDTO.OwnerID); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } if (authenticatedUser != user) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (companyProfileDTO.URI != null) { Regex regex = new Regex("[^a-zA-Z0-9_-]"); companyProfileDTO.URI = companyProfileDTO.URI.Replace(" ", "-"); companyProfileDTO.URI = regex.Replace(companyProfileDTO.URI, "").ToLower(); if (companyProfileDTO.URI.Length == 0) { companyProfileDTO.URI = null; } else if (db.Companies.Where(company => company.URI == companyProfileDTO.URI && company.ID != companyProfileDTO.ID).SingleOrDefault() != null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } } else if (companyProfileDTO.ID == 0 && companyProfileDTO.URI == null) { Regex regex = new Regex("[^a-zA-Z0-9_-]"); companyProfileDTO.URI = companyProfileDTO.DisplayName.Replace(" ", "-"); companyProfileDTO.URI = regex.Replace(companyProfileDTO.URI, "").ToLower(); } user.Company = Mapper.Map(companyProfileDTO, user.Company); db.Entry(user).State = EntityState.Modified; await db.SaveChangesAsync(); if (companyProfileDTO.Countries != null) { if (db.CompanyCountries.Any(cc => cc.CompanyId == user.Company.ID)) { db.CompanyCountries.RemoveRange(db.CompanyCountries.Where(cc => cc.CompanyId == user.Company.ID)); await db.SaveChangesAsync(); } List <CompanyCountry> countries = new List <CompanyCountry>(); foreach (string country in companyProfileDTO.Countries) { var countryModel = db.Countries.SingleOrDefault(c => c.Name == country); if (countryModel != null) { CompanyCountry companyCountry = new CompanyCountry(); companyCountry.CompanyId = user.Company.ID; companyCountry.CountryId = countryModel.Id; countries.Add(companyCountry); } } db.CompanyCountries.AddRange(countries); await db.SaveChangesAsync(); } if (!db.Sectors.Any(s => s.SectorName == companyProfileDTO.Sector)) { Sector sector = new Sector(); sector.Active = true; sector.CreateDate = DateTime.Now; sector.SectorName = companyProfileDTO.Sector; db.Sectors.Add(sector); await db.SaveChangesAsync(); } bool existingSkills = db.CompanySkills.Any(cs => cs.CompanyId == user.Company.ID); if (existingSkills) { //remove it List <CompanySkill> companySkills = db.CompanySkills.Where(cs => cs.CompanyId == user.Company.ID).ToList(); db.CompanySkills.RemoveRange(companySkills); db.SaveChanges(); } if (companyProfileDTO.Skills != null && companyProfileDTO.Skills.Count() > 0 && user.Company.ID > 0) { bool hasSkill = false; foreach (string skill in companyProfileDTO.Skills) { CompanySkill existing = db.CompanySkills.SingleOrDefault(cs => cs.SkillName == skill && cs.CompanyId == user.Company.ID); if (existing == null) { if (!db.Skills.Any(s => s.SkillName == skill)) { Skill skillMaster = new Skill(); skillMaster.SkillName = skill; skillMaster.Active = true; skillMaster.CreateDate = DateTime.Now; db.Skills.Add(skillMaster); db.SaveChanges(); } CompanySkill newSkill = new CompanySkill(); newSkill.CompanyId = user.Company.ID; newSkill.SkillName = skill; db.CompanySkills.Add(newSkill); hasSkill = true; } } if (hasSkill) { await db.SaveChangesAsync(); } } if (!user.OnboardingSkipped) { List <CompanyMember> companyMembers = await db.CompanyMembers.Where(c1 => c1.UserID == user.Id).ToListAsync(); if (companyMembers != null && companyMembers.Count > 0) { CompanyMember cm = companyMembers.FirstOrDefault(); if (cm != null) { db.CompanyMembers.Remove(cm); await db.SaveChangesAsync(); } } } return(Ok(Mapper.Map <Company, CompanyProfileDTO>(user.Company))); }
public APIGatewayProxyResponse SaveCompany(APIGatewayProxyRequest request, ILambdaContext context) { try { context.Logger.LogLine("Get Request\n"); if (!ValidateRequest(request)) { return(new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.Forbidden, Headers = new Dictionary <string, string> { { "Content-Type", "text/plain" } } }); } if (string.IsNullOrEmpty(request.Body)) { throw new ArgumentNullException("request.Body"); } Company source = JsonConvert.DeserializeObject <Company>(request.Body); Company targetCompany; SilverContext silverContext = new SilverContext(); if (!source.Id.HasValue || source.Id == 0) { targetCompany = new Company(); silverContext.Companies.Add(targetCompany); } else { targetCompany = silverContext.Companies.Where(x => x.Id == source.Id) .Include(x => x.CompanyCountries) .Include(x => x.CompanyExtendedData) .Include(x => x.CompanyQuestionnaires) .Include(x => x.CompanyIndustries) .Include(x => x.CompanyNames) .FirstOrDefault(); if (targetCompany == null) { throw new ArgumentOutOfRangeException("company"); } } targetCompany.Lei = source.Lei; targetCompany.Status = source.Status; targetCompany.LegalName = source.LegalName; targetCompany.NumEmployees = source.NumEmployees; // countries var srcIds = source.CompanyCountries .Where(x => x.CompanyCountryId.HasValue) .Select(x => x.CompanyCountryId.Value) .ToHashSet(); var toRemove = new List <CompanyCountry>(); foreach (var companyCountry in targetCompany.CompanyCountries) { if (!srcIds.Contains(companyCountry.CompanyCountryId.Value)) { toRemove.Add(companyCountry); } } foreach (var item in toRemove) { targetCompany.CompanyCountries.Remove(item); silverContext.Remove(item); } foreach (var companyCountry in source.CompanyCountries) { CompanyCountry targetEntity; if (!companyCountry.CompanyCountryId.HasValue) { targetEntity = new CompanyCountry() { Company = targetCompany }; targetCompany.CompanyCountries.Add(targetEntity); silverContext.CompanyCountries.Add(targetEntity); } else { targetEntity = targetCompany.CompanyCountries.First(x => x.CompanyCountryId == companyCountry.CompanyCountryId); } targetEntity.CountryId = companyCountry.CountryId; targetEntity.IsPrimary = companyCountry.IsPrimary; targetEntity.LegalJurisdiction = companyCountry.LegalJurisdiction; targetEntity.Ticker = companyCountry.Ticker; } // extended data if (source.CompanyExtendedData.Count > 0) { var sourceExtendedData = source.CompanyExtendedData.First(); CompanyExtendedData targetExtendedData; if (targetCompany.CompanyExtendedData.Any()) { targetExtendedData = targetCompany.CompanyExtendedData.First(); } else { targetExtendedData = new CompanyExtendedData(); targetCompany.CompanyExtendedData.Add(targetExtendedData); silverContext.Add(targetExtendedData); } targetExtendedData.BelowNationalAvgIncome = sourceExtendedData.BelowNationalAvgIncome; targetExtendedData.Company = targetCompany; targetExtendedData.DisabledEmployees = sourceExtendedData.DisabledEmployees; targetExtendedData.HierarchyLevel = sourceExtendedData.HierarchyLevel; targetExtendedData.RetentionRate = sourceExtendedData.RetentionRate; targetExtendedData.MedianSalary = sourceExtendedData.MedianSalary; } else { targetCompany.CompanyExtendedData.Clear(); } // questions if (source.CompanyQuestionnaires.Count > 0) { srcIds = source.CompanyQuestionnaires .Where(x => x.Id.HasValue) .Select(x => x.Id.Value) .ToHashSet(); var questionsToRemove = new List <CompanyQuestion>(); foreach (var companyQuestion in targetCompany.CompanyQuestionnaires) { if (!srcIds.Contains(companyQuestion.Id.Value)) { questionsToRemove.Add(companyQuestion); } } foreach (var item in questionsToRemove) { targetCompany.CompanyQuestionnaires.Remove(item); silverContext.Remove(item); } foreach (var companyQuestion in source.CompanyQuestionnaires) { CompanyQuestion targetEntity; if (!companyQuestion.Id.HasValue) { targetEntity = new CompanyQuestion() { Company = targetCompany }; targetCompany.CompanyQuestionnaires.Add(targetEntity); silverContext.CompanyQuestionnaires.Add(targetEntity); } else { targetEntity = targetCompany.CompanyQuestionnaires.First(x => x.Id == companyQuestion.Id); } targetEntity.Question = companyQuestion.Question; targetEntity.Answer = companyQuestion.Answer; } } else { targetCompany.CompanyQuestionnaires.Clear(); } // names if (source.CompanyNames.Count > 0) { srcIds = source.CompanyNames .Where(x => x.Id.HasValue) .Select(x => x.Id.Value) .ToHashSet(); var namesToRemove = new List <CompanyName>(); foreach (var companyName in targetCompany.CompanyNames) { if (!srcIds.Contains(companyName.Id.Value)) { namesToRemove.Add(companyName); } } foreach (var item in namesToRemove) { targetCompany.CompanyNames.Remove(item); silverContext.Remove(item); } foreach (var companyName in source.CompanyNames) { CompanyName targetEntity; if (!companyName.Id.HasValue) { targetEntity = new CompanyName() { Company = targetCompany }; targetCompany.CompanyNames.Add(targetEntity); silverContext.CompanyNames.Add(targetEntity); } else { targetEntity = targetCompany.CompanyNames.First(x => x.Id == companyName.Id); } targetEntity.Name = companyName.Name; targetEntity.Type = companyName.Type; } } else { targetCompany.CompanyNames.Clear(); } // industries if (source.CompanyIndustries.Count > 0) { srcIds = source.CompanyIndustries .Where(x => x.Id.HasValue) .Select(x => x.Id.Value) .ToHashSet(); var industriesToRemove = new List <CompanyIndustry>(); foreach (var companyName in targetCompany.CompanyIndustries) { if (!srcIds.Contains(companyName.Id.Value)) { industriesToRemove.Add(companyName); } } foreach (var item in industriesToRemove) { targetCompany.CompanyIndustries.Remove(item); silverContext.Remove(item); } foreach (var companyIndustry in source.CompanyIndustries) { CompanyIndustry targetEntity; if (!companyIndustry.Id.HasValue) { targetEntity = new CompanyIndustry() { Company = targetCompany }; targetCompany.CompanyIndustries.Add(targetEntity); silverContext.CompanyIndustries.Add(targetEntity); } else { targetEntity = targetCompany.CompanyIndustries.First(x => x.Id == companyIndustry.Id); } targetEntity.IndustryId = companyIndustry.IndustryId; targetEntity.PrimarySecondary = companyIndustry.PrimarySecondary; } } else { targetCompany.CompanyIndustries.Clear(); } // TODO: roles and people silverContext.SaveChanges(); var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Headers = new Dictionary <string, string> { { "Content-Type", "text/plain" } } }; return(response); } catch (Exception ex) { return(new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.BadRequest, Body = $"Bad query: {ex}", Headers = new Dictionary <string, string> { { "Content-Type", "text/plain" } } }); } }