Esempio n. 1
0
        public ActionResult CreateGuild(int?id)
        {
            // Check if the ID is null
            if (id == null)
            {
                return(View("~/Views/Shared/InvalidResource.cshtml", model: "character"));
            }

            string username = User.Identity.GetUserId();
            // Make sure the authenticated user is valid
            var user = _authRepository.GetUserAccount(username);

            if (user == null)
            {
                HttpContext.GetOwinContext().Authentication.SignOut();
                return(RedirectToAction("Index", "Home"));
            }

            var character = _authUserCharRepository.Get(username, id);

            if (character == null)
            {
                return(View("~/Views/Shared/InvalidResource.cshtml", model: "character"));
            }

            var model = new CreateGuildVM()
            {
                ShardId             = character.ShardId,
                ShardName           = character.Shard.Name,
                AuthUserCharacterId = (int)id
            };

            return(View(model));
        }
Esempio n. 2
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));
        }