Example #1
0
        public ActionResult CreateGuild(CreateGuildVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string email = User.Identity.GetUserId();
            // Remove leading and trailing spaces from the guild so we don't end up with duplicates
            string guildName = model.Name.Trim();

            #region Logout and redirect if the logged in user is invalid
            if (_authRepository.GetUserAccount(email) == null)
            {
                HttpContext.GetOwinContext().Authentication.SignOut();
                return(RedirectToAction("Index", "Home"));
            }
            #endregion
            // Get the default guild rank before the actual create happens, otherwise we could end up with
            // a guild that we can't link to a user (if there's no default rank)
            var defaultRank = _guildRankRepository.GetDefaultRankForGuildCreators();
            if (defaultRank != null)
            {
                var result = _guildRepository.Create(email, guildName, model.ShardId);
                if (result.Success)
                {
                    // The PK (ID) of the new guild is in result.Message (string format)
                    int newGuildId = int.Parse(result.Message);

                    result = _authUserCharRepository.AddCharacterToGuild(model.AuthUserCharacterId, newGuildId, defaultRank.Id);
                    if (result.Success)
                    {
                        return(RedirectToAction("GuildCreated", new { @guildName = guildName.ToUpper() }));
                    }
                }

                ModelState.AddModelError("", result.Message);
            }
            else
            {
                ModelState.AddModelError("", "The guild could not be created as no default guild rank has been selected! Please let the site operators know ASAP.");
            }

            return(View(model));
        }