Exemple #1
0
        // GET /api/users/5
        public HttpResponseMessage <UserModels.UserResponse> Get(string id)
        {
            var user = GetUser(id);

            //TODO: check to make sure user exists
            if (user == null)
            {
                var message = new HttpResponseMessage <UserModels.UserResponse>(HttpStatusCode.NotFound);
                message.ReasonPhrase = "User Not Found";

                return(message);
            }

            var userResponse = new UserModels.UserResponse()
            {
                Address                          = user.Address,
                City                             = user.City,
                createDate                       = user.CreateDate,
                Culture                          = user.Culture,
                emailAddress                     = user.EmailAddress,
                FirstName                        = user.FirstName,
                isConfirmed                      = user.IsConfirmed,
                IsLockedOut                      = user.IsLockedOut,
                LastLoggedIn                     = user.LastLoggedIn,
                LastName                         = user.LastName,
                lastPasswordFailureDate          = user.LastPasswordFailureDate,
                MobileNumber                     = user.MobileNumber,
                passwordFailuresSinceLastSuccess = user.PasswordFailuresSinceLastSuccess,
                SenderName                       = user.SenderName,
                State                            = user.State,
                TimeZone                         = user.TimeZone,
                userId                           = user.UserId,
                userName                         = user.UserName,
                UserStatus                       = user.UserStatus.ToString(),
                Zip            = user.Zip,
                UserAttributes = user.UserAttributes.Select(a => new UserModels.UserAttribute()
                {
                    AttributeName  = a.UserAttribute.AttributeName,
                    AttributeValue = a.AttributeValue
                }).ToList()
            };

            return(new HttpResponseMessage <UserModels.UserResponse>(userResponse, HttpStatusCode.OK));
        }
        // GET /api/users/5
        public HttpResponseMessage <UserModels.UserResponse> Get(string id)
        {
            _logger.Log(LogLevel.Info, String.Format("Getting User {0}", id));

            DomainServices.UserService _userService = new DomainServices.UserService(_ctx);

            User user = null;

            try
            {
                user = _userService.GetUserById(id);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Info, String.Format("Unable to find user by id {0}. {1}", id, ex.Message));
            }

            if (user == null)
            {
                var message = new HttpResponseMessage <UserModels.UserResponse>(HttpStatusCode.NotFound);
                message.ReasonPhrase = "User Not Found";

                return(message);
            }


            double sentTotal     = 0;
            double receivedTotal = 0;

            var sentPayments = _ctx.Messages
                               .Where(m => m.SenderId.Equals(user.UserId) && m.MessageTypeValue.Equals((int)MessageType.Payment));

            if (sentPayments.Count() > 0)
            {
                sentTotal = sentPayments.Sum(m => m.Amount);
            }

            var receivedPayments = _ctx.Messages
                                   .Where(m => m.RecipientId.Value.Equals(user.UserId) && m.MessageTypeValue.Equals((int)MessageType.Payment));

            if (receivedPayments.Count() > 0)
            {
                receivedTotal = receivedPayments.Sum(m => m.Amount);
            }

            _logger.Log(LogLevel.Info, String.Format("User Mobile Number {0}", user.MobileNumber));

            UserModels.UserResponse userResponse = null;

            try
            {
                userResponse = new UserModels.UserResponse()
                {
                    address                          = user.Address,
                    city                             = user.City,
                    createDate                       = user.CreateDate.Value.ToString("ddd MMM dd HH:mm:ss zzz yyyy"),
                    culture                          = user.Culture,
                    emailAddress                     = user.EmailAddress,
                    firstName                        = user.FirstName,
                    isConfirmed                      = user.IsConfirmed,
                    isLockedOut                      = user.IsLockedOut,
                    lastLoggedIn                     = user.LastLoggedIn.ToString("ddd MMM dd HH:mm:ss zzz yyyy"),
                    lastName                         = user.LastName,
                    lastPasswordFailureDate          = user.LastPasswordFailureDate,
                    mobileNumber                     = user.MobileNumber,
                    passwordFailuresSinceLastSuccess = user.PasswordFailuresSinceLastSuccess,
                    senderName                       = user.SenderName,
                    state                            = user.State,
                    timeZone                         = user.TimeZone,
                    userId                           = user.UserId,
                    userName                         = user.UserName,
                    userStatus                       = user.UserStatus.ToString(),
                    zip            = user.Zip,
                    userAttributes = user.UserAttributes.Select(a => new UserModels.UserAttribute()
                    {
                        AttributeName  = a.UserAttribute.AttributeName,
                        AttributeValue = a.AttributeValue
                    }).ToList(),
                    upperLimit         = user.Limit,
                    totalMoneyReceived = receivedTotal,
                    totalMoneySent     = sentTotal
                };
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;

                _logger.ErrorException(String.Format("Unhandled exception formatting User Response {0}. {1}", id, errorMessage), ex);

                throw new HttpResponseException(errorMessage, HttpStatusCode.InternalServerError);
            }

            return(new HttpResponseMessage <UserModels.UserResponse>(userResponse, HttpStatusCode.OK));
        }