public async Task <IQueryable <CompanyFollowerDTO> > GetMyCompanysFollowers() { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).Include(_user => _user.Company).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (user.Company == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } IQueryable <CompanyConnection> companyConnections = db.CompanyConnections .Where(connection => connection.CompanyID == user.Company.ID) .Include(connection => connection.User) .Include(connection => connection.Company) .Include(connection => connection.FollowerGroups); IQueryable <CompanyFollowerDTO> companyFollowerDtos = companyConnections.ProjectTo <CompanyFollowerDTO>(); foreach (CompanyFollowerDTO companyFollowerDTO in companyFollowerDtos) { companyFollowerDTO.IsNew = (user.LastCheckForConnectionRequests == null) || ((companyFollowerDTO.Status == ConnectionStatus.Pending) && (user.LastCheckForConnectionRequests < companyFollowerDTO.RequestedAt)); } user.LastCheckForConnectionRequests = DateTime.Now; db.Entry(user).State = EntityState.Modified; await db.SaveChangesAsync(); return(companyFollowerDtos); }
public async Task <IHttpActionResult> UploadProductFiles(NewProductFileDTO[] newProductFiles) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } for (int i = 0; i < newProductFiles.Length; i++) { long productId = newProductFiles[i].ProductID; string visibility = newProductFiles[i].Visibility; Product product = db.Products.Find(productId); if (product == null) { return(NotFound()); } if ((product.CompanyID.HasValue && product.CompanyID != user.Company.ID) && (product.TeamMembers.Where(teamMember => teamMember.UserID == user.Id && teamMember.CanEditTheProduct == true).SingleOrDefault() == null)) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } ProductFile productFile = Mapper.Map <NewProductFileDTO, ProductFile>(newProductFiles[i]); ProductUpdate productUpdate = new ProductUpdate(); productFile.Product = product; productFile.Type = ProductFileType.Other; productFile.UploadedAt = DateTime.Now; if (!string.IsNullOrEmpty(visibility) && visibility.ToLower() == "private") { productFile.Privacy = ProductFilePrivacy.Private; } productFile = db.ProductFiles.Add(productFile); productUpdate.UserID = user.Id; productUpdate.User = user; productUpdate.ProductID = product.ID; productUpdate.Product = product; productUpdate.ProductFileID = productFile.ID; productUpdate.ProductFile = productFile; productUpdate.DateTime = DateTime.Now; productUpdate.UpdateType = UpdateType.ProductFileAdded; db.ProductUpdates.Add(productUpdate); } await db.SaveChangesAsync(); return(Ok()); }
public async Task <IHttpActionResult> AddNewMemberToMyCompany(NewCompanyMemberDTO newCompanyMemberDTO) { 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.Where(_user => _user.UserName == newCompanyMemberDTO.UserName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } Company company = authenticatedUser.Company; if (company == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } CompanyMember companyMember = db.CompanyMembers.Where(_companyMember => _companyMember.UserID == user.Id && _companyMember.CompanyID == company.ID).SingleOrDefault(); if (companyMember != null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } UserLevel editorUserLevel = await db.UserLevels.SingleOrDefaultAsync(ul => ul.Name == "Editor"); companyMember = new CompanyMember(); companyMember.UserID = user.Id; companyMember.CompanyID = company.ID; companyMember.User = user; companyMember.Company = authenticatedUser.Company; companyMember.Role = newCompanyMemberDTO.Role; if (editorUserLevel != null) { companyMember.UserLevelId = editorUserLevel.Id; } companyMember = db.CompanyMembers.Add(companyMember); await db.SaveChangesAsync(); return(Ok(Mapper.Map <CompanyMember, CompanyMemberDTO>(companyMember))); }
public async Task <IHttpActionResult> NewNewsItem(NewNewsItemDTO newNewsItemDTO) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Product product = null; if (newNewsItemDTO.ProductID != null) { product = db.Products.Where(_product => _product.ID == newNewsItemDTO.ProductID) .Include(_product => _product.TeamMembers .Select(teamMember => teamMember.User) .Select(teamMember => teamMember.Company)) .SingleOrDefault(); if (product == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } if ((product.CompanyID != user.Company.ID) && (product.TeamMembers.Where(teamMember => teamMember.UserID == user.Id && teamMember.CanEditTheProduct == true).SingleOrDefault() == null)) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } } NewsItem newsItem = Mapper.Map <NewNewsItemDTO, NewsItem>(newNewsItemDTO); if (product != null) { newsItem.ProductID = product.ID; newsItem.Product = product; } newsItem.UserID = user.Id; newsItem.User = user; newsItem.PostedAt = DateTime.Now; newsItem.UpdatedAt = DateTime.Now; newsItem = db.NewsItems.Add(newsItem); await db.SaveChangesAsync(); return(Ok(Mapper.Map <NewsItem, NewsItemDTO>(newsItem))); }
public async Task <IHttpActionResult> NewSkill(SkillDTO newSkillDTO) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Skill exist = db.Skills.SingleOrDefault(sec => sec.SkillName == newSkillDTO.SkillName); if (exist != null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } newSkillDTO.Active = true; newSkillDTO.CreateDate = DateTime.Now; Skill skill = Mapper.Map <SkillDTO, Skill>(newSkillDTO); db.Skills.Add(skill); await db.SaveChangesAsync(); return(Ok(Mapper.Map <Skill, SkillDTO>(skill))); }
public async Task <IHttpActionResult> GetProduct(long id) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } Product product = db.Products.Where(_product => _product.ID == id) .Include(_product => _product.TeamMembers) .Include(_product => _product.GroupsVisibleTo) .SingleOrDefault(); if (product == null) { return(NotFound()); } if ((product.CompanyID != user.Company.ID) && (product.TeamMembers.Where(teamMember => teamMember.UserID == user.Id && teamMember.CanEditTheProduct == true).SingleOrDefault() == null)) { if (product.Privacy == ProductPrivacy.Private) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if ((product.Privacy == ProductPrivacy.VisibleToSelectedGroups) && product.GroupsVisibleTo.Contains(db.CompanyFollowerGroups.FirstOrDefault(followerGroup => followerGroup.CompanyID == product.CompanyID && followerGroup.Followers.Any(follower => follower.UserID == user.Id))) == false) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } ProductView productView = new ProductView(); productView.UserID = user.Id; productView.User = user; productView.ProductID = product.ID; productView.Product = product; productView.DateTime = DateTime.Now; db.ProductViews.Add(productView); await db.SaveChangesAsync(); } return(Ok(Mapper.Map <Product, ProductViewDTO>(product))); }
public async Task <IHttpActionResult> NewCustomer(NewCustomerDTO newCustomerDTO) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Product product = db.Products.Find(newCustomerDTO.ProductID); if (product == null) { return(NotFound()); } if ((product.CompanyID != user.Company.ID) && (product.TeamMembers.Where(teamMember => teamMember.UserID == user.Id && teamMember.CanEditTheProduct == true).SingleOrDefault() == null)) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } Customer customer = Mapper.Map <NewCustomerDTO, Customer>(newCustomerDTO); ProductUpdate productUpdate = new ProductUpdate(); customer.Product = product; customer = db.Customers.Add(customer); productUpdate.UserID = user.Id; productUpdate.User = user; productUpdate.ProductID = product.ID; productUpdate.Product = product; productUpdate.DateTime = DateTime.Now; productUpdate.UpdateType = UpdateType.ProductCustomerAdded; db.ProductUpdates.Add(productUpdate); await db.SaveChangesAsync(); return(Ok(Mapper.Map <Customer, CustomerDTO>(customer))); }
public long Insert(InviteRequest request) { if (request == null) { return(0); } if (string.IsNullOrEmpty(request.InviteToEmailAddress)) { return(0); } db.InviteRequests.Add(request); db.SaveChangesAsync(); return(request.Id); }
public async Task <IEnumerable <ProductUpdateDTO> > GetLatestProductUpdatesPaged(int pageNumber, int pageSize) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (pageNumber <= 0 || pageSize <= 0) { throw new HttpResponseException(HttpStatusCode.BadRequest); } IQueryable <ProductUpdate> productUpdates = db.ProductUpdates.Where(productUpdate => (productUpdate.Product.Company.Owner.Id == user.Id) || (productUpdate.Product.TeamMembers.Any(teamMember => teamMember.UserID == user.Id)) || (productUpdate.Product.Company.Followers.Any(follower => follower.UserID == user.Id) && ((productUpdate.Product.Privacy == ProductPrivacy.Public) || (productUpdate.Product.Privacy == ProductPrivacy.VisibleToSelectedGroups && productUpdate.Product.GroupsVisibleTo.Any(followerGroup => followerGroup.Followers.Any(follower => follower.UserID == user.Id)))) && ((productUpdate.ProductFile == null) || ((productUpdate.ProductFile != null) && ((productUpdate.ProductFile.Privacy == ProductFilePrivacy.Public) || (productUpdate.ProductFile.Privacy == ProductFilePrivacy.VisibleToSelectedGroups && productUpdate.ProductFile.GroupsVisibleTo.Any(followerGroup => followerGroup.Followers.Any(follower => follower.UserID == user.Id)))))))) .OrderByDescending(productUpdate => productUpdate.DateTime); IEnumerable <ProductUpdateDTO> productUpdateDtos = Mapper.Map <IEnumerable <ProductUpdateDTO> >(productUpdates.ToPagedList(pageNumber, pageSize).ToList()); foreach (ProductUpdateDTO productUpdateDto in productUpdateDtos) { productUpdateDto.IsNew = (user.LastCheckForProductUpdates == null) || (user.LastCheckForProductUpdates < productUpdateDto.DateTime); } user.LastCheckForProductUpdates = DateTime.Now; db.Entry(user).State = EntityState.Modified; await db.SaveChangesAsync(); return(productUpdateDtos); }
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 async Task <IHttpActionResult> AddNewProductTeamMember(NewProductTeamMemberDTO newProductTeamMemberDTO) { 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)); } Product product = db.Products.Where(_product => _product.ID == newProductTeamMemberDTO.ProductID) .Include(_product => _product.TeamMembers .Select(teamMember => teamMember.User) .Select(teamMember => teamMember.Company)) .SingleOrDefault(); if (product == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } if ((product.CompanyID != authenticatedUser.Company.ID) && (product.TeamMembers.Where(teamMember => teamMember.UserID == authenticatedUser.Id && teamMember.CanEditTheProduct == true).SingleOrDefault() == null)) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } User productTeamMemberUser = db.Users.Where(_user => _user.Id == newProductTeamMemberDTO.UserID) .Include(_user => _user.CompaniesAsMembers) .Include(_user => _user.ProductTeamMembers) .SingleOrDefault(); if (productTeamMemberUser == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } Company company = product.Company; if (company == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } CompanyMember companyMember = productTeamMemberUser.CompaniesAsMembers.Where(_companyMember => _companyMember.CompanyID == company.ID).SingleOrDefault(); if (companyMember == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } ProductTeamMember productTeamMember = productTeamMemberUser.ProductTeamMembers.Where(_productTeamMember => _productTeamMember.ProductID == newProductTeamMemberDTO.ProductID).SingleOrDefault(); if (productTeamMember != null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } productTeamMember = new ProductTeamMember(); productTeamMember.UserID = productTeamMemberUser.Id; productTeamMember.ProductID = product.ID; productTeamMember.User = productTeamMemberUser; productTeamMember.Product = product; productTeamMember.CanEditTheProduct = false; productTeamMember = db.ProductTeamMembers.Add(productTeamMember); await db.SaveChangesAsync(); return(Ok(Mapper.Map <ProductTeamMember, ProductTeamMemberDTO>(productTeamMember))); }
public async Task <IHttpActionResult> NewCompanyFollowerGroup(NewCompanyFollowerGroupDTO newCompanyFollowerGroupDTO) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Company company = user.Company; CompanyFollowerGroup companyFollowerGroup = new CompanyFollowerGroup(); companyFollowerGroup.CompanyID = company.ID; companyFollowerGroup.Company = company; companyFollowerGroup.Name = newCompanyFollowerGroupDTO.Name; companyFollowerGroup = db.CompanyFollowerGroups.Add(companyFollowerGroup); await db.SaveChangesAsync(); return(Ok(Mapper.Map <CompanyFollowerGroup, CompanyFollowerGroupDTO>(companyFollowerGroup))); }
public async Task <IHttpActionResult> JumpToWizardStep(UserOnboardingStatusDTO inputStep) { string userName = User.Identity.Name; User user = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault(); if (user == null) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (!inputStep.OnboardingStep.HasValue) { throw new HttpResponseException(HttpStatusCode.BadRequest); } if (inputStep.OnboardingStep.HasValue && (inputStep.OnboardingStep.Value < 0 || inputStep.OnboardingStep.Value > 4)) { throw new HttpResponseException(HttpStatusCode.BadRequest); } user.OnboardingStep = inputStep.OnboardingStep.Value; db.Entry(user).State = EntityState.Modified; await db.SaveChangesAsync(); return(Ok(Mapper.Map <User, UserOnboardingStatusDTO>(user))); }