Example #1
0
        public ActionResult Test()
        {
            Response.Write("HomeController Test start...");

            using (ChatContext context = new ChatContext())
            {
                User user = new User();
                user.Name = "Piotrek - " + Guid.NewGuid().ToString();
                user.LastActivity = DateTime.Now;
                user.HubConnections.Add(new HubConnection() { ConnectionId = Guid.NewGuid().ToString().Substring(2, 10) });
                user.HubConnections.Add(new HubConnection() { ConnectionId = Guid.NewGuid().ToString().Substring(2, 10) });
                context.Entry(user).State = EntityState.Added;
                context.SaveChanges();
            }

            using (ChatContext context = new ChatContext())
            {
                foreach (var user in context.Users)
                {
                    Response.Write("<br />" + user.Name);

                    if (user.HubConnections.Count() > 0)
                    {
                        Response.Write("<br />  User's hub connections (" + user.HubConnections.Count() + ")");
                        foreach (var conn in user.HubConnections)
                        {
                            Response.Write("<br />     - " + conn.ConnectionId);
                        }
                    }
                }
            }

            return new EmptyResult();
        }
Example #2
0
        public ActionResult Create(ChatUser model)
        {
            AjaxTypedResult<ChatUser> result = new AjaxTypedResult<ChatUser>();

            if (!User.Identity.IsAuthenticated && ModelState.IsValid)
            {
                using (var context = new ChatContext())
                {
                    User user = context.Users.FirstOrDefault(x => x.Name == model.Name);

                    if (user == null)
                    {
                        User newUser = new User()
                        {
                            Name = model.Name,
                            LastActivity = DateTime.Now
                        };
                        context.Entry(newUser).State = EntityState.Added;
                        context.SaveChanges();

                        result.Model = new ChatUser()
                        {
                            Name = newUser.Name,
                            Id = newUser.Id
                        };
                        result.Success = true;
                        result.SuccessMessage = "Użytkownik został dodany.";
                        FormsAuthentication.SetAuthCookie(model.Name, true);
                    }
                    else
                        result.Errors.Add("Wystąpił błąd. Nazwa użytkownika jest zajęta.");
                }
            }
            else
                result.Errors.Add("Wystąpił błąd. Użytkownik jest już zalogowany lub nazwa jest niepoprawna.");

            return this.JsonCamelCase(result);
        }