Пример #1
0
        public object DeletePlace(int id)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    if (!_placeService.TryRemovePlace(id))
                    {
                        responseModel.FromErrorCode(ErrorList.ErrorCode.PlaceNotFound);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorList.ErrorCode.Success);
                    responseModel.Data = null;
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetPlaceById(int id, [FromQuery] int page, [FromQuery] int pageSize)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var errorCode = _placeService.TryGetPlaceInfoById(id, out var place);

                    if (!errorCode)
                    {
                        responseModel.FromErrorCode(ErrorList.ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorList.ErrorCode.Success);
                    responseModel.Data = new JArray {
                        place.ToJson()
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object DeleteCommentPost(int id, int commentId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    if (!_postService.GetPostById(id, out var post))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    if (!_postService.RemoveComment(commentId))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray();
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #4
0
        public object RemoveFriend(int friendId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    var errorCode = _friendService.TryRemoveFriend(userId, friendId);
                    if (errorCode != ErrorCode.Success)
                    {
                        responseModel.FromErrorCode(errorCode);
                        break;
                    }

                    responseModel.FromErrorCode(errorCode);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #5
0
        public object GetMyPosts(int userId, [FromQuery] int page, [FromQuery] int pageSize)
        {
            var responseModel = new ResponseModel();

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

                    if (!_postService.GetPostByUserId_join(myUserId, userId, page, pageSize, out var posts, out var pagination))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(posts);
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetTourFeedbacks(int tourId, [FromQuery] int page, [FromQuery] int pageSize,
                                       [FromQuery] string sortBy, [FromQuery] string orderBy)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    if (!_tourService.GetFeedbacks(userId, tourId, sortBy, orderBy, page, pageSize, out var feedbacks,
                                                   out var pagination))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(feedbacks);
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetTourDetail(int tourId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    if (!_tourService.TryGetTour(userId, tourId, out var tour))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        JObject.FromObject(tour)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetPostById(int id)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    if (!_postService.GetPostById(id, out var post))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray()
                    {
                        JObject.FromObject(post)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #9
0
        public object GetMyTourInfos(int userId, [FromQuery] int page, [FromQuery] int pageSize)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    CoreHelper.ValidatePageSize(ref page, ref pageSize);

                    _tourInfoService.TryGetTourInfosByUserId(userId, page, pageSize, out var tourInfos,
                                                             out var pagination);

                    if (tourInfos == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = tourInfos.Count > 0
                        ? JArray.FromObject(tourInfos.Select(t => t.ToJson()))
                        : new JArray();
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetListTour([FromQuery] int page, [FromQuery] int pageSize, [FromQuery] string type)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);
                    List <SimpleTour> tours;
                    Pagination        pagination;

                    switch (type.ToLower())
                    {
                    case "recommend":
                        if (!_tourService.GetTourListRecommend(userId, page, pageSize, out tours, out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;

                    case "friend":
                        if (!_tourService.GetTourListFriend(userId, page, pageSize, out tours, out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;

                    case "foryou":
                        if (!_tourService.GetTourListForYou(userId, page, pageSize, out tours, out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;

                    default:
                        if (!_tourService.GetTourListRecommend(userId, page, pageSize, out tours, out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(tours);
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetTourMembers(int tourId, [FromQuery] int page, [FromQuery] int pageSize,
                                     [FromQuery] string type)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);
                    List <SimpleTourMember> members;
                    Pagination pagination;

                    switch (type.ToLower())
                    {
                    case "accepted":
                        if (!_tourService.TryGetTourAcceptedMembers(userId, tourId, page, pageSize, out members,
                                                                    out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;

                    case "requesting":
                        if (!_tourService.TryGetTourRequestedMembers(userId, tourId, page, pageSize, out members,
                                                                     out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;

                    default:
                        if (!_tourService.TryGetTourAllMembers(userId, tourId, page, pageSize, out members,
                                                               out pagination))
                        {
                            responseModel.FromErrorCode(ErrorCode.Fail);
                        }

                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(members);
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object RemoveMember(int userId, int tourId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    // Get user id
                    var myUserId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    // Check user is exist
                    if (!_userService.TryGetUsers(userId, out var user))
                    {
                        responseModel.FromErrorCode(ErrorCode.UserNotFound);
                        break;
                    }

                    // Add tour to tour info
                    if (!_tourService.TryGetTour(myUserId, tourId, out var tour))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    // User doest not have permission
                    if (myUserId != tour.CreateById)
                    {
                        Response.StatusCode = 403;
                        responseModel.FromErrorCode(ErrorCode.UserDoesNotHavePermission);
                    }

                    // Add join tour
                    if (!_tourService.TryRemoveTourMember(tour, user))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        JObject.FromObject(tour)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #13
0
        public object CommentPost(int id, [FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    var body = requestBody != null
                        ? JObject.Parse(requestBody.ToString() ?? "{}")
                        : null;

                    if (!CoreHelper.GetParameter(out var jsonContent, body, "Content",
                                                 JTokenType.String, ref responseModel, true))
                    {
                        break;
                    }

                    var content = jsonContent?.ToString();

                    if (!_postService.GetPostById(id, out var post))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    var postComment = new PostComment(content, userId, id, DateTime.Now);

                    if (!_postService.CommentPost(postComment))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray()
                    {
                        JObject.FromObject(postComment)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #14
0
        public object GetTourInfo(int id, int tourId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    if (_tourInfoService.TryGetTourInfoById(id, out var tourInfo) != ErrorCode.Success ||
                        tourInfo == null)
                    {
                        throw new ExceptionWithMessage("Tour info not found.");
                    }

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

                    if (!_tourService.TryGetTour(userId, tourId, out var tour) || tour == null)
                    {
                        throw new ExceptionWithMessage("Tour not found.");
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        JObject.FromObject(tour)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #15
0
        public object GetTourServices(int tourInfoId, int tourId, [FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    if (_tourInfoService.TryGetTourInfoById(tourInfoId, out var tourInfo) != ErrorCode.Success ||
                        tourInfo == null)
                    {
                        throw new ExceptionWithMessage("Tour info not found.");
                    }

                    if (_serviceService.TryGetServiceByTourId(tourId, out var tourServices))
                    {
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(tourServices);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #16
0
        public object GetServiceById(int id)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    if (_serviceService.TryGetServiceById(id, out var service))
                    {
                        break;
                    }

                    if (service == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.ServiceNotFound);
                        break;
                    }

                    responseModel.ErrorCode = (int)ErrorCode.Success;
                    responseModel.Message   = Description(responseModel.ErrorCode);
                    responseModel.Data      = new JArray {
                        service.ToJson()
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #17
0
        public object DeleteTour(int id, int tourId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    if (_tourInfoService.TryGetTourInfoById(id, out var tourInfo) != ErrorCode.Success ||
                        tourInfo == null)
                    {
                        throw new ExceptionWithMessage("Tour info not found.");
                    }

                    if (!_tourService.TryDeleteTour(tourId))
                    {
                        throw new ExceptionWithMessage("Tour not found.");
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray();
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #18
0
        public object CreateTourService([FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var body = requestBody != null
                        ? JObject.Parse(requestBody.ToString() ?? "{}")
                        : null;

                    if (!CoreHelper.GetParameter(out var jsonName, body, "name",
                                                 JTokenType.String, ref responseModel) ||
                        !CoreHelper.GetParameter(out var jsonImage, body, "image",
                                                 JTokenType.String, ref responseModel, true))
                    {
                        break;
                    }

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

                    var tourService = new Service(
                        name: jsonName.ToString(),
                        image: jsonImage?.ToString(),
                        deletedAt: null
                        );

                    if (!_serviceService.TryAddService(tourService, userId))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        tourService.ToJson()
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #19
0
        public object UpdateTourService(int id, [FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var body = requestBody != null
                        ? JObject.Parse(requestBody.ToString() ?? "{}")
                        : null;

                    if (!CoreHelper.GetParameter(out var jsonName, body, "Name",
                                                 JTokenType.String, ref responseModel, isNullable: true) ||
                        !CoreHelper.GetParameter(out var jsonImages, body, "Image",
                                                 JTokenType.String, ref responseModel, isNullable: true))
                    {
                        break;
                    }

                    if (!_serviceService.TryGetServiceById(id, out var service) || service == null)
                    {
                        throw new ExceptionWithMessage("Service not found");
                    }

                    service.Update(jsonName?.ToString() !, jsonImages?.ToString() !);

                    // Add tour to tour info
                    if (!_serviceService.TryUpdateService(service))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        service.ToJson()
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object JoinTour(int tourId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    // Get user id
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    // Check user is exist
                    if (!_userService.TryGetUsers(userId, out var user))
                    {
                        responseModel.FromErrorCode(ErrorCode.UserNotFound);
                        break;
                    }

                    // Add tour to tour info
                    if (!_tourService.TryGetTour(userId, tourId, out var tour))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    // Add join tour
                    if (!_tourService.TryJoinTour(tour, user))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        JObject.FromObject(tour)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #21
0
        public object GetMyFriends([FromQuery] string type, [FromQuery] int page, [FromQuery] int pageSize)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    CoreHelper.ValidatePageSize(ref page, ref pageSize);

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

                    var friendType = type?.ToLower() switch
                    {
                        FriendType.Accepted => FriendType.Accepted,
                        FriendType.Requested => FriendType.Requested,
                        FriendType.Waiting => FriendType.Waiting,
                        _ => FriendType.Accepted
                    };

                    if (!_userService.TryGetFriends(userId, friendType, page, pageSize, out var friends,
                                                    out var pagination) || friends == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    var listFriendSimple = friends.Select(u => u.ToSimpleUser(friendType))?.ToList() ??
                                           new List <SimpleUser>();

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(listFriendSimple);
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object GetAllPlaces([FromQuery] int page, [FromQuery] int pageSize, [FromQuery] string type, [FromQuery] string keyword)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var errorCode = _placeService.TryGetAllPlaces(page, pageSize, type, keyword, out var places, out var pagination);

                    if (!errorCode)
                    {
                        responseModel.FromErrorCode(ErrorList.ErrorCode.Fail);
                        break;
                    }

                    var jPlaces = new JArray();

                    foreach (var p in places)
                    {
                        var isSuccess = _placeService.TryGetPlaceInfoById(p.Id, out var place);
                        if (!isSuccess)
                        {
                            continue;
                        }
                        jPlaces.Add(place.ToJson());
                    }

                    responseModel.FromErrorCode(ErrorList.ErrorCode.Success);
                    responseModel.Data = jPlaces;
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #23
0
        public object GetMyInfo()
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var identity = HttpContext.User.Identity as ClaimsIdentity;
                    if (identity == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    var claims = identity.Claims;
                    int.TryParse(claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value,
                                 out var userId);
                    _userService.TryGetUsers(userId, out var user);

                    if (user == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray
                    {
                        JObject.FromObject(user)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #24
0
        public object GetUserFriends(int id, [FromQuery] int page, [FromQuery] int pageSize)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    CoreHelper.ValidatePageSize(ref page, ref pageSize);

                    if (!(HttpContext.User.Identity is ClaimsIdentity identity))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    if (!_userService.TryGetFriends(id, FriendType.Accepted, page, pageSize, out var friends,
                                                    out var pagination) || friends == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    // Remove me from list
                    friends.RemoveAll(u => u.Id == id);
                    var listFriendSimple = friends.Select(u => u.ToSimpleJson(FriendType.Accepted));

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = JArray.FromObject(listFriendSimple);
                    responseModel.AdditionalProperties["Pagination"] = JObject.FromObject(pagination);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #25
0
        public object GetTourInfo(int id)
        {
            ResponseModel responseModel = new ResponseModel();

            try
            {
                do
                {
                    var errorCode = _tourInfoService.TryGetTourInfoById(id, out var tourInfo);
                    if (errorCode != ErrorCode.Success)
                    {
                        responseModel.FromErrorCode(errorCode);
                        break;
                    }

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

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

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        AddTourFullInfo(tourInfo, myUserId)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #26
0
        public static int GetUserId(HttpContext requestContext, ref ResponseModel responseModel)
        {
            var identity = requestContext.User.Identity as ClaimsIdentity;

            if (identity == null)
            {
                responseModel.FromErrorCode(ErrorList.ErrorCode.Fail);
            }

            var claims = identity.Claims;

            int.TryParse(claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value,
                         out var userId);
            return(userId);
        }
Пример #27
0
        public object AddFriend(int userToRequestId)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var identity = HttpContext.User.Identity as ClaimsIdentity;
                    if (identity == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    var claims = identity.Claims;
                    int.TryParse(claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value,
                                 out var userId);

                    var errorCode = _friendService.TryAddFriend(userId, userToRequestId);
                    if (errorCode != ErrorCode.Success)
                    {
                        responseModel.FromErrorCode(errorCode);
                        break;
                    }

                    responseModel.FromErrorCode(errorCode);
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
        public object UpdateUser(int userId, [FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var body = requestBody != null
                        ? JObject.Parse(requestBody.ToString() !)
                        : null;

                    if (!CoreHelper.GetParameter(out var jsonPassword, body, "password", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonEmail, body, "email", JTokenType.String,
                                                 ref responseModel) ||
                        !CoreHelper.GetParameter(out var jsonName, body, "name", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonPhoneNumber, body, "phoneNumber", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonAddress, body, "address", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonAvatar, body, "avatar", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonSlogan, body, "slogan", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonBio, body, "bio", JTokenType.String, ref responseModel,
                                                 true) ||
                        !CoreHelper.GetParameter(out var jsonJob, body, "job", JTokenType.String, ref responseModel,
                                                 true) ||
                        !CoreHelper.GetParameter(out var jsonGender, body, "gender", JTokenType.String,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonBirthday, body, "birthday", JTokenType.Date,
                                                 ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonRole, body, "role", JTokenType.String,
                                                 ref responseModel, true))
                    {
                        break;
                    }

                    var isParsedBirthday = DateTime.TryParse(jsonBirthday?.ToString(), out var birthday);

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

                    if (user == null)
                    {
                        responseModel.FromErrorCode(ErrorCode.UserNotFound);
                        break;
                    }

                    user.Update(name: jsonName?.ToString(),
                                password: jsonPassword?.ToString(),
                                email: jsonEmail?.ToString() !, phoneNumber: jsonPhoneNumber?.ToString(), avatar: jsonAvatar?.ToString(), bio: jsonBio?.ToString(), slogan: jsonSlogan?.ToString(), job: jsonJob?.ToString(), birthday: isParsedBirthday ? birthday : (DateTime?)null, gender: jsonGender?.ToString(), address: jsonAddress?.ToString(), role: jsonRole?.ToString());

                    var isSuccess = _userService.TryUpdateUser(user);

                    if (!isSuccess)
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray {
                        JObject.FromObject(user)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #29
0
        public object UpdatePost(int id, [FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var userId = CoreHelper.GetUserId(HttpContext, ref responseModel);

                    var body = requestBody != null
                        ? JObject.Parse(requestBody.ToString() ?? "{}")
                        : null;

                    if (!CoreHelper.GetParameter(out var jsonContent, body, "Content",
                                                 JTokenType.String, ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonImages, body, "Images",
                                                 JTokenType.Array, ref responseModel, true) ||
                        !CoreHelper.GetParameter(out var jsonRating, body, "Rating",
                                                 JTokenType.Float, ref responseModel, isNullable: true))
                    {
                        break;
                    }

                    var isRatingParsed =
                        float.TryParse(jsonRating?.ToString(), out var rating);

                    var content = jsonContent?.ToString();
                    var images  = jsonImages != null
                        ? JsonConvert.DeserializeObject <string[]>(jsonImages.ToString())
                        : null;

                    if (!_postService.GetPostById(id, out var post))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    if (post.AuthorId != userId)
                    {
                        throw new ExceptionWithMessage("You dont have permission");
                    }

                    post.Update(
                        content: content,
                        images: images !,
                        rating: isRatingParsed ? rating : (float?)null
                        );

                    if (!_postService.Update(post))
                    {
                        responseModel.FromErrorCode(ErrorCode.Fail);
                        break;
                    }

                    responseModel.FromErrorCode(ErrorCode.Success);
                    responseModel.Data = new JArray()
                    {
                        JObject.FromObject(post)
                    };
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }
Пример #30
0
        public object SocialProvider([FromBody] object requestBody)
        {
            var responseModel = new ResponseModel();

            try
            {
                do
                {
                    var body = requestBody != null
                        ? JObject.Parse(requestBody.ToString() !)
                        : null;

                    if (!CoreHelper.GetParameter(out var jsonAccessToken, body, "AccessToken", JTokenType.String,
                                                 ref responseModel) ||
                        !CoreHelper.GetParameter(out var jsonType, body, "Type", JTokenType.String,
                                                 ref responseModel) ||
                        !CoreHelper.GetParameter(out var jsonEmail, body, "Email", JTokenType.String,
                                                 ref responseModel, isNullable: true))
                    {
                        break;
                    }

                    string accessToken = jsonAccessToken.ToString();
                    string type        = jsonType.ToString();
                    string email       = (jsonEmail ?? "").ToString();

                    if (type.Equals("facebook", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!_userService.TryGetFacebookInfo(accessToken, out AuthProvider authProvider))
                        {
                            break;
                        }

                        JArray data = new JArray();

                        ErrorCode errorCode = _userService.Authenticate(email, ref authProvider, out User user);

                        if (errorCode == ErrorCode.Success)
                        {
                            data.Add(UserResponseJson(user));
                            responseModel.ErrorCode = (int)ErrorCode.Success;
                            responseModel.Message   = Description(responseModel.ErrorCode);
                            responseModel.Data      = data;
                        }
                        else
                        {
                            responseModel.FromErrorCode(errorCode);
                        }
                    }
                    else
                    {
                        responseModel.FromErrorCode(ErrorCode.FeatureIsBeingImplemented);
                    }
                } while (false);
            }
            catch (Exception ex)
            {
                responseModel.FromException(ex);
            }

            return(responseModel.ToJson());
        }