public bool FollowerRegister(int id, string emailAddress, string firstName, string lastName, string company = "", string apikey = "") { if (User.Identity.Name != null) if (!OPIMsys.Filters.ApiKeyHandler.ApiKeyToUser(apikey, Request)) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Forbidden)); if (!Roles.IsUserInRole("ReportAPI") && !Roles.IsUserInRole("ApiReadUser")) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Forbidden)); AccountApiKey apiUser = OPIMsys.Filters.ApiKeyHandler.KeyToAccount(apikey, Request); if (!Roles.IsUserInRole("ReportAPI")) if (id != apiUser.CompanyId) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Forbidden)); OPIMsys.RegexUtilities regUtil = new RegexUtilities(); if (!regUtil.IsValidEmail(emailAddress)) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Conflict)); if(db.CompanyFollowers.Where(a => a.EmailAddress == emailAddress).Count() > 0) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Conflict)); Company comp = db.Companies.Find(id); if (comp == null) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); CompanyFollower follow = new CompanyFollower(); follow.EmailAddress = emailAddress; follow.FirstName = firstName; follow.LastName = lastName; follow.CompanyName = company; comp.CompanyFollowers.Add(follow); db.SaveChanges(); return true; }
public void CreateCompanyFollower(int companyId, int userId) { if (companyId != userId) { var isFollowed = _companyFollowerRepository.Get(x => x.UserId == userId && x.CompanyUserId == companyId); if (isFollowed != null) { return; } var companyFollower = new CompanyFollower(); companyFollower.UserId = userId; companyFollower.CompanyUserId = companyId; companyFollower.CreateDate = DateTime.Now; _companyFollowerRepository.Add(companyFollower); try { _companyFollowerRepository.SaveChanges(); } catch (Exception ex) { var errorMessage = ex.Message; throw; } } }