예제 #1
0
        public object GetUserById(int userId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var data = new JArray();

                    var myUserId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    if (!_userService.TryGetUsers(userId, out var user))
                    {
                        break;
                    }

                    if (user == null)
                    {
                        responseModel.ErrorCode = (int)ErrorCode.UserNotFound;
                        responseModel.Message   = Description(responseModel.ErrorCode);
                        break;
                    }

                    var friendType = _friendService.CalculateIsFriend(myUserId, userId);

                    data.Add(user.ToSimpleJson(friendType));
                    responseModel.ErrorCode = (int)ErrorCode.Success;
                    responseModel.Message   = Description(responseModel.ErrorCode);
                    responseModel.Data      = data;
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
예제 #2
0
        public object GetAllTours(int id, [FromQuery] int page, [FromQuery] int pageSize)
        {
            var responseModel = new ResponseModel();

            var data = new JArray();

            try
            {
                do
                {
                    var errorCode = _tourInfoService.TryGetTours(id, page, pageSize, out var tours, out var pagination);

                    if (errorCode != ErrorCode.Success)
                    {
                        responseModel.FromErrorCode(errorCode);
                        break;
                    }

                    errorCode = _tourInfoService.TryGetTourInfoById(id, out var tourInfo);

                    if (errorCode != ErrorCode.Success)
                    {
                        responseModel.FromErrorCode(errorCode);
                        break;
                    }

                    if (tourInfo == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.TourNotFound);
                        break;
                    }

                    if (!_userService.TryGetUsers(tourInfo.CreateById, out var host))
                    {
                        responseModel.FromErrorCode(ErrorCode.UserNotFound);
                        break;
                    }

                    // Cast to ClaimsIdentity.
                    var identity = HttpContext.User.Identity as ClaimsIdentity;

                    // Gets list of claims.
                    IEnumerable <Claim> claim = identity?.Claims;

                    // Gets userId from claims. Generally it's an email address.
                    var userIdString = claim
                                       ?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)
                                       ?.Value;

                    if (string.IsNullOrEmpty(userIdString))
                    {
                        responseModel.FromErrorCode(ErrorCode.UserNotFound);
                        break;
                    }

                    // Convert userId to int
                    var userId = int.Parse(userIdString);

                    var eIsFriend = _friendService.CalculateIsFriend(userId, host.Id);

                    // Add data to Response
                    foreach (var tour in tours)
                    {
                        _ = _tourService.TryGetTotalMember(tour.Id, out var totalMember);

                        data.Add(tour.ToSimpleJson(host, eIsFriend, totalMember, tourInfo));
                    }

                    responseModel.ErrorCode = (int)ErrorCode.Success;
                    responseModel.Message   = Description(responseModel.ErrorCode);
                    responseModel.Data      = data;
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }