// POST api/<controller> // // Add user to the database // public HttpResponseMessage Post([FromBody] JsonUser value) { if (value == null) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid user information"); } Models.Repository repository = new Models.Repository(); Facebook.FacebookUser fbUser = new Facebook.FacebookUser { id = value.UserId, email = value.Email, first_name = value.FirstName, last_name = value.LastName, name = value.Name }; var mfUser = repository.CreateUser(fbUser); repository.SaveChanges(); List <GroupMember> membership = repository.UserMembership(mfUser); if ((membership == null) || (membership.Count == 0)) { Group defaultGroup = repository.FindGroupByCode("JYMF"); repository.DefaultGroup(mfUser, defaultGroup, GroupRoleEnum.member); repository.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.Created, "User added to application")); }
// POST api/<controller> // // Add user to the database // public HttpResponseMessage Post([FromBody] JsonUser value) { if (value == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid user information")); } Models.Repository repository = new Models.Repository(); var user = repository.GetUserByEmail(value.Email); if (user != null) { return(Request.CreateErrorResponse(HttpStatusCode.Conflict, "User with same email already exists")); } string groupCode = HttpContext.Current.Request.QueryString["code"]; Models.Group defaultGroup = repository.FindGroupByCode((string.IsNullOrEmpty(groupCode) ? "JYMF" : groupCode)); if (defaultGroup == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Group code not found")); } string userid; do { Random random = new Random(); userid = (!string.IsNullOrEmpty(value.UserId) ? value.UserId : System.Web.Security.Membership.GeneratePassword(16, 0)); userid = Regex.Replace(userid, @"[^a-zA-Z0-9]", m => random.Next(0, 9).ToString()); user = repository.GetUserById(userid); } while (user != null); MFUser fbUser = new MFUser { UserId = userid, Email = value.Email, Password = value.Password, FirstName = value.FirstName, LastName = value.LastName, Name = value.Name }; var mfUser = repository.CreateUser(fbUser); repository.SaveChanges(); if (repository.IsUserInGroup(mfUser, defaultGroup) == GroupRoleEnum.empty) { repository.DefaultGroup(mfUser, defaultGroup, GroupRoleEnum.member); repository.SaveChanges(); } // Send an email notification // var smtp = new SmtpClient(); // Settings in config file var message = new MailMessage("*****@*****.**", ConfigurationManager.AppSettings["AdminEmail"]); message.Subject = "JYMF RaceDay New User"; message.IsBodyHtml = true; message.Priority = MailPriority.High; message.Body = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/NewUser.txt")); message.Body = message.Body.Replace("@FIRSTNAME@", fbUser.FirstName) .Replace("@LASTNAME@", fbUser.LastName) .Replace("@EMAIL@", fbUser.Email); smtp.Send(message); return(Request.CreateResponse(HttpStatusCode.Created, "User added to application")); }