Beispiel #1
0
        public HttpResponseMessage PostRegisterUser([FromBody]UserDto userDto)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    if (userDto == null)
                    {
                        throw new ArgumentException("Cannot register an empty user.");
                    }

                    this.ValidateUsername(userDto.Username);
                    this.ValidateNickname(userDto.Nickname);
                    this.ValidateAuthCode(userDto.AuthCode);

                    var db = new ForumContext();

                    var user = db.Users.Where(u =>
                        u.Username.ToLower() == userDto.Username.ToLower() || u.Nickname.ToLower() == userDto.Nickname.ToLower())
                        .FirstOrDefault();

                    if (user != null)
                    {
                        throw new ApplicationException("User exist.");
                    }

                    user = new User()
                    {
                        Nickname = userDto.Nickname,
                        Username = userDto.Username,
                        AuthCode = userDto.AuthCode
                    };

                    db.Users.Add(user);
                    db.SaveChanges();

                    string sessionKey = this.GenerateSessionKey(user.Id);
                    user.SessionKey = sessionKey;
                    db.SaveChanges();

                    var userLogedIn = new UserLogedDto()
                    {
                        Nickname = user.Nickname,
                        SessionKey = sessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                                            userLogedIn);

                    return response;
                });

            return responseMsg;
        }
Beispiel #2
0
        public HttpResponseMessage Get([FromUri]string sessionKey, [FromUri]string category)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    if (sessionKey.Length != SessionKeyLength)
                    {
                        throw new ArgumentException("Invalid session key.");
                    }

                    var db = new ForumContext();

                    var user = db.Users.Where(u => u.SessionKey == sessionKey).FirstOrDefault();

                    if (user == null)
                    {
                        throw new ApplicationException("Invalin session. Try to login.");
                    }

                    var allThreads = (from t in db.Threads
                                      where t.Categories.Any(c => c.Name == category)
                                      select new ThreadDto()
                                      {
                                          Id = t.Id,
                                          Title = t.Title,
                                          Content = t.Content,
                                          CreatedBy = t.User.Username,
                                          DateCreated = t.DateCreated,
                                          Categories = t.Categories.Select(c => c.Name),
                                          Posts = (from p in t.Posts
                                                   select new PostDto()
                                                   {
                                                       Id = p.Id,
                                                       Content = p.Content,
                                                       PostDate = p.PostDate,
                                                       PostedBy = p.User.Username
                                                   })
                                      });

                    var response = this.Request.CreateResponse(HttpStatusCode.OK,
                                            allThreads.OrderByDescending(d => d.DateCreated));

                    return response;
                });

            return responseMsg;
        }
Beispiel #3
0
        public void LogoutUser([FromBody]UserLogedDto userLogedDto)
        {
            if (userLogedDto == null)
            {
                throw new ArgumentException("Cannot logout an empty user.");
            }

            var db = new ForumContext();

            var user = db.Users.Where(u => u.SessionKey == userLogedDto.SessionKey).FirstOrDefault();

            if (user == null)
            {
                throw new ApplicationException("Invalid session key.");
            }

            user.SessionKey = null;
            db.SaveChanges();
        }