public object GetUserById(int userId) { var responseModel = new ResponseModel(); try { do { var data = new JArray(); if (!_userService.TryGetUsers(userId, out var user)) { break; } if (user == null) { responseModel.ErrorCode = (int)ErrorCode.UserNotFound; responseModel.Message = Description(responseModel.ErrorCode); break; } data.Add(JObject.FromObject(user)); responseModel.ErrorCode = (int)ErrorCode.Success; responseModel.Message = Description(responseModel.ErrorCode); responseModel.Data = data; } while (false); } catch (Exception ex) { responseModel.FromException(ex); } return(responseModel.ToJson()); }
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 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 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 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()); }
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()); }
public object Authenticate([FromBody] AuthenticateModel model) { var responseModel = new ResponseModel(); var data = new JArray(); try { var errorCode = _userService.Authenticate(model.Email, model.Password, out User user); if (user == null || errorCode != ErrorCode.Success) { responseModel.ErrorCode = (int)ErrorCode.UsernamePasswordIncorrect; responseModel.Message = Description(responseModel.ErrorCode); } else { data.Add(UserResponseJson(user)); responseModel.ErrorCode = (int)ErrorCode.Success; responseModel.Message = Description(responseModel.ErrorCode); responseModel.Data = data; } } 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 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 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()); }
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()); }
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 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()); }
public object DeleteUser(int userId) { var responseModel = new ResponseModel(); try { do { if (!_userService.TryRemoveUser(userId)) { responseModel.ErrorCode = (int)ErrorCode.Fail; responseModel.Message = "Remove user fail"; break; } responseModel.ErrorCode = (int)ErrorCode.Success; responseModel.Message = Description(responseModel.ErrorCode); } 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()); }
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()); }
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()); }
public object Register([FromBody] object requestBody) { var response = new ResponseModel(); try { do { var data = new JArray(); // Parse request body to json var reqBody = requestBody != null ? JObject.Parse(requestBody.ToString() !) : null; if (!CoreHelper.GetParameter(out var jsonName, reqBody, "Name", JTokenType.String, ref response) || !CoreHelper.GetParameter(out var jsonPhoneNumber, reqBody, "PhoneNumber", JTokenType.String, ref response) || !CoreHelper.GetParameter(out var jsonEmail, reqBody, "Email", JTokenType.String, ref response) || !CoreHelper.GetParameter(out var jsonPassword, reqBody, "Password", JTokenType.String, ref response) ) { break; } var name = jsonName.ToString(); var email = jsonEmail.ToString(); var phoneNumber = jsonPhoneNumber.ToString(); var password = jsonPassword.ToString(); var statusCode = _userService.Register(name, email, phoneNumber, password, out var user); if (statusCode == ErrorCode.Success) { data.Add(UserResponseJson(user)); } response.ErrorCode = (int)statusCode; response.Message = Description(response.ErrorCode); response.Data = data; } while (false); } catch (Exception ex) { response.FromException(ex); } return(response.ToJson()); }
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()); }
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()); }
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()); }
public object GetAllTourInfos([FromQuery] int page, [FromQuery] int pageSize) { var responseModel = new ResponseModel(); try { do { var data = new JArray(); if (_tourInfoService.TryGetTourInfos(page, pageSize, out var tourInfos, out var pagination) != ErrorCode.Success) { break; } if (tourInfos == null || tourInfos.Count == 0 || pagination == null) { responseModel.ErrorCode = (int)ErrorCode.UserNotFound; responseModel.Message = Description(responseModel.ErrorCode); break; } var myUserId = CoreHelper.GetUserId(HttpContext, ref responseModel); // Add data to Response foreach (var tourInfo in tourInfos) { data.Add(AddTourFullInfo(tourInfo, myUserId)); } 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()); }
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 GetAllUser([FromQuery] int page, [FromQuery] int pageSize) { var responseModel = new ResponseModel(); try { do { var data = new JArray(); if (!_userService.TryGetUsers(page, pageSize, out var users, out var pagination)) { break; } if (users == null || users.Count == 0 || pagination == null) { responseModel.ErrorCode = (int)ErrorCode.UserNotFound; responseModel.Message = Description(responseModel.ErrorCode); break; } foreach (var user in users) { data.Add(JObject.FromObject(user)); } 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()); }
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()); }
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()); }
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()); }