public bool TryGetTourAllMembers(int myUserId, int tourId, int page, int pageSize, out List <SimpleTourMember> users, out Pagination pagination) { try { DbService.ConnectDb(out _context); var members = (from tm in _context.TourMembers join u in _context.Users on tm.UserId equals u.Id where (tm.TourId == tourId && tm.DeletedAt == null) select new { User = u, TourMember = tm, }); var total = members?.Count() ?? 0; var skip = pageSize * (page - 1); var canPage = skip < total; if (canPage) { members = pageSize <= 0 ? members : members .Skip(skip) .Take(pageSize); // Sort by AcceptedAt members = members.OrderByDescending(e => e.TourMember.AcceptedAt); users = members.AsEnumerable().Select(e => { var friendType = _friendService.CalculateIsFriend(myUserId, e.User.Id); return(new SimpleTourMember( e.User.Id, e.User.Name, e.User.Avatar, e.User.Job, friendType, e.TourMember.JoinAt, e.TourMember.AcceptedAt )); }).ToList(); } else { users = new List <SimpleTourMember>(); } pagination = new Pagination(total, page, pageSize); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetServiceCreateByUser(int userId, out List <Service> tourServices) { var isSuccess = false; tourServices = new List <Service>(); do { try { DbService.ConnectDb(out _context); tourServices = (from serviceDetail in _context.ServiceDetails where serviceDetail.CreateById == userId join service in _context.Services on serviceDetail.Id equals service.Id select service)?.ToList(); isSuccess = true; } finally { DbService.DisconnectDb(ref _context); } } while (false); return(isSuccess); }
public bool TryAddPlace(Place place, int?parentId) { try { DbService.ConnectDb(out _context); if (parentId != null) { _ = _context.Places.FirstOrDefault(p => p.Id == parentId) ?? throw new ExceptionWithMessage("Parent place not found"); } place = _context.Places.Add(place).Entity; _context.SaveChanges(); var childPlaceRecord = _context.ChildPlaces.FirstOrDefault(p => p.ChildId == place.Id && p.ParentId == parentId); if (childPlaceRecord == null && parentId != null) { _context.ChildPlaces.Add(new ChildPlace((int)parentId, place.Id)); } } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetTimelines(int tourId, out List <TimeLine> timelines) { try { DbService.ConnectDb(out _context); var tour = _context.Tours.SingleOrDefault(t => t.Id == tourId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Tour not found"); timelines = _context.TimeLines.Where(t => t.TourId == tourId && t.DeletedAt == null)?.ToList() ?? new List <TimeLine>(); foreach (var timeLine in timelines) { var timelineDetails = _context.TimelineDetails.Where(td => td.TimelineId == timeLine.Id && td.DeletedAt == null); timeLine.TimelineDetails ??= new List <TimelineDetail>(); timeLine.TimelineDetails.AddRange(timelineDetails); } _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetTimelines(int tourId, out List <TimeLine> timelines) { try { DbService.ConnectDb(out _context); var _ = _context.Tours.SingleOrDefault(t => t.Id == tourId) ?? throw new ExceptionWithMessage("Tour not found"); timelines = _context.TimeLines.Where(t => t.TourId == tourId)?.ToList() ?? new List <TimeLine>(); foreach (var timeLine in timelines) { var timelineDetails = _context.TimelineDetails.Where(td => td.TimelineId == timeLine.Id).ToList(); for (int i = 0; i < timelineDetails.Count; i++) { var place = _context.Places.FirstOrDefault(p => p.Id == timelineDetails[i].PlaceId); timelineDetails[i].Place = place; } timeLine.TimelineDetails ??= new List <TimelineDetail>(); timeLine.TimelineDetails.AddRange(timelineDetails); } } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool RemoveComment(int postCommentId) { try { DbService.ConnectDb(out _context); var postComment = _context.PostComments.FirstOrDefault(p => p.Id == postCommentId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("Comment not found"); var post = _context.Posts.FirstOrDefault(p => p.Id == postComment.PostId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("Post not found"); var user = _context.Users.FirstOrDefault(u => u.Id == postComment.UserId && post.DeletedAt == null) ?? throw new ExceptionWithMessage("User not found"); postComment.Delete(); post.TotalComment--; _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool AddNewPost(Post post) { try { DbService.ConnectDb(out _context); if (post.TourId != null) { var tour = _context.Tours.FirstOrDefault(t => t.Id == post.TourId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Tour not found"); var tourInfo = _context.TourInfos.FirstOrDefault(ti => ti.Id == tour.TourInfoId) ?? throw new ExceptionWithMessage("Tour info not found"); tourInfo.Rating ??= 0; tourInfo.Rating = (tourInfo.TotalRating * tourInfo.Rating + post.Rating) / (tourInfo.TotalRating + 1); tourInfo.TotalRating++; } else { post.Rating = null; } _context.Posts.Add(post); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetUsers(string email, out User user) { user = null; var isSuccess = false; try { DbService.ConnectDb(out _context); user = (from u in _context.Users where (u.Email == email) select u) .FirstOrDefaultAsync() .Result; // Lấy Product có ID chỉ ra if (user != null) { isSuccess = true; } } finally { DbService.DisconnectDb(ref _context); } return(isSuccess); }
public bool CommentPost(PostComment postComment) { try { DbService.ConnectDb(out _context); var post = _context.Posts.FirstOrDefault(p => p.Id == postComment.PostId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("Post not found"); var user = _context.Users.FirstOrDefault(p => p.Id == postComment.UserId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("User not found"); _context.PostComments.Add(postComment); post.TotalComment++; _context.SaveChanges(); var friendType = _friendService.CalculateIsFriend(post.AuthorId, post.AuthorId); postComment.Author = user.ToSimpleUser(friendType); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetTour(int userId, int id, out Tour tour) { try { DbService.ConnectDb(out _context); tour = _context.Tours.SingleOrDefault(t => t.Id == id && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Tour not found"); var createById = tour.CreateById; var createBy = _context.Users.FirstOrDefault(u => u.Id == createById); var tourMember = _context.TourMembers.FirstOrDefault(tm => tm.TourId == id && tm.UserId == userId); var friendType = _friendService.CalculateIsFriend(userId, createById); TryGetTourInfo(userId, id, out var tourInfo); TryGetTimelines(id, out var timeLines); tour.TimeLines = timeLines; tour.TourInfo = tourInfo; tour.CreateBy = createBy?.ToSimpleUser(friendType); tour.JoinAt = tourMember?.JoinAt; tour.AcceptedAt = tourMember?.AcceptedAt; tour.DeletedAt = tourMember?.DeletedAt; } finally { DbService.DisconnectDb(ref _context); } return(true); }
public ErrorCode TryRemoveFriend(int userId, int userRequestId) { ErrorCode errorCode; do { try { DbService.ConnectDb(out _context); TryGetFriendRequest(userId, userRequestId, out var friendDb); if (friendDb == null) { throw new ExceptionWithMessage("Friend request not found"); } friendDb.Delete(); _context.SaveChanges(); errorCode = ErrorCode.Success; } finally { DbService.DisconnectDb(ref _context); } } while (false); return(errorCode); }
public bool TryJoinTour(Tour tour, User user) { try { DbService.ConnectDb(out _context); var tourMember = _context.TourMembers.FirstOrDefault(t => t.TourId == tour.Id && t.UserId == user.Id && t.DeletedAt == null); if (tour.CreateById == user.Id) { throw new ExceptionWithMessage("Cannot perform request, you are creator"); } if (tourMember != null) { var exception = tourMember.AcceptedAt != null ? new ExceptionWithMessage("You have already joined the tour") : new ExceptionWithMessage("You have already requested to join the tour"); throw exception; } _context.TourMembers.Add(new TourMember(tour.Id, user.Id)); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool GetPostByTourId(int tourId, out Post post) { try { DbService.ConnectDb(out _context); var _ = _context.Tours.FirstOrDefault(t => t.Id == tourId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Tour not found"); var postDb = _context.Posts.FirstOrDefault(p => p.TourId == tourId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("Post not found"); post = postDb; var postComments = _context.PostComments.Where(pc => pc.PostId == postDb.Id) .OrderByDescending(pc => pc.CreateAt)?.ToList(); post.FeaturedComment = postComments != null ? postComments[0] : null; } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryAddAuthProvider(AuthProvider authProvider, User user) { try { DbService.ConnectDb(out _context); var dbAuthProvider = _context.AuthProviders.FirstOrDefault(a => a.Id == authProvider.Id); if (dbAuthProvider != null) { _context.AuthProviders.Add(authProvider); _context.AuthProviders.Update(dbAuthProvider); } else { authProvider.UserId = user.Id; _context.AuthProviders.Add(authProvider); } _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryAcceptJoinTour(Tour tour, User user) { try { DbService.ConnectDb(out _context); var tourMember = _context.TourMembers.FirstOrDefault(t => t.TourId == tour.Id && t.UserId == user.Id && t.AcceptedAt == null && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Request not found"); var totalMember = _context.TourMembers.Count(t => t.TourId == tour.Id && t.AcceptedAt != null); if (totalMember >= tour.MaxMember) { throw new ExceptionWithMessage("The members are maximum"); } tourMember.AcceptedAt = DateTime.Now; _context.TourMembers.Update(tourMember); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetTourInfo(int userId, int tourId, out TourInfo tourInfo) { try { DbService.ConnectDb(out _context); var tour = _context.Tours.SingleOrDefault(t => t.Id == tourId) ?? throw new ExceptionWithMessage("Tour not found"); tourInfo = _context.TourInfos.FirstOrDefault(t => t.Id == tour.TourInfoId); var tourInfoDb = tourInfo; var startPlace = _context.Places.FirstOrDefault(p => p.Id == tourInfoDb.StartPlaceId); var destinationPlace = _context.Places.FirstOrDefault(p => p.Id == tourInfoDb.DestinatePlaceId); var createBy = _context.Users.FirstOrDefault(u => u.Id == tourInfoDb.CreateById); var friendType = _friendService.CalculateIsFriend(userId, createBy.Id); tourInfo.StartPlace = startPlace; tourInfo.DestinatePlace = destinationPlace; tourInfo.CreateBy = createBy.ToSimpleUser(friendType); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool DislikePost(int postId, int userId) { try { DbService.ConnectDb(out _context); var post = _context.Posts.FirstOrDefault(p => p.Id == postId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("Post not found"); var user = _context.Users.FirstOrDefault(p => p.Id == userId && p.DeletedAt == null) ?? throw new ExceptionWithMessage("User not found"); var postLike = _context.PostLikes.FirstOrDefault(p => p.PostId == postId && p.UserId == userId) ?? throw new ExceptionWithMessage("You did not like the post yet"); if (postLike.DeletedAt == null) { postLike.DeletedAt = DateTime.Now; post.TotalLike--; } else { throw new ExceptionWithMessage("You did not like the post yet"); } _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetFriends(int userId, string type, int page, int pageSize, out List <User> friends, out Pagination pagination) { friends = new List <User>(); pagination = null; var isSuccess = false; try { DbService.ConnectDb(out _context); var friendDBs = type?.ToLower() switch { FriendType.Accepted => _context.Friends.Where(a => a.AcceptedAt != null && (a.UserId == userId || a.RequestedUserId == userId)).ToArray(), FriendType.Requested => _context.Friends .Where(a => a.AcceptedAt == null && (a.RequestedUserId == userId)) .ToArray(), FriendType.Waiting => _context.Friends.Where(a => a.AcceptedAt == null && (a.UserId == userId)) .ToArray(), _ => _context.Friends.Where(a => a.UserId == userId || a.RequestedUserId == userId).ToArray() }; var total = friendDBs.Count(); var skip = pageSize * (page - 1); var canPage = skip < total; if (canPage) { var listFriends = pageSize <= 0 ? friendDBs.ToList() : friendDBs.Select(u => u) .Skip(skip) .Take(pageSize) .ToList(); foreach (var friend in listFriends) { var id = friend.UserId != userId ? friend.UserId : friend.RequestedUserId; TryGetUsers(id, out var user); friends.Add(user); } friends = friends.Distinct().ToList(); } else { friends = new List <User>(); } pagination = new Pagination(total, page, pageSize); isSuccess = true; } finally { DbService.DisconnectDb(ref _context); } return(isSuccess); }
public bool GetUserComment(int userId, int postId, int page, int pageSize, out List <PostComment> postComments, out Pagination pagination) { try { DbService.ConnectDb(out _context); var _ = _context.Posts.FirstOrDefault(t => t.Id == postId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Post not found"); var userComments = (from pc in _context.PostComments join user in _context.Users on pc.UserId equals user.Id where pc.DeletedAt == null && pc.PostId == postId select new { user, pc }).OrderByDescending(a => a.pc.Id).ToList(); var postCommentDb = new List <PostComment>(); foreach (var userComment in userComments) { var friendType = _friendService.CalculateIsFriend(userId, userComment.user.Id); userComment.pc.Author = userComment.user.ToSimpleUser(friendType); postCommentDb.Add(userComment.pc); } var total = postCommentDb.Count(); var skip = pageSize * (page - 1); var canPage = skip < total; List <User> resultUsers; if (canPage) { postComments = pageSize <= 0 ? postCommentDb : postCommentDb .Skip(skip) .Take(pageSize) .ToList(); } else { postComments = new List <PostComment>(); } pagination = new Pagination(total, page, pageSize > 0 ? pageSize : total); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public ErrorCode TryGetTourInfosByUserId(int userId, int page, int pageSize, out List <TourInfo> tourInfos, out Pagination pagination) { tourInfos = null; pagination = null; ErrorCode errorCode; try { ValidatePageSize(ref page, ref pageSize); DbService.ConnectDb(out _context); var listTourInfos = _context.TourInfos.Where(a => a.CreateById == userId && a.DeletedAt == null) .OrderByDescending(a => a.Id) .ToList(); var total = listTourInfos.Select(p => p.Id).Count(); var skip = pageSize * (page - 1); var canPage = skip < total; if (canPage) { // If pageSize <= 0 then get all tour info tourInfos = pageSize <= 0 ? listTourInfos : listTourInfos .Skip(skip) .Take(pageSize) .ToList(); } else { tourInfos = new List <TourInfo>(); } foreach (var tourInfo in tourInfos) { TryGetPlaceById(tourInfo.StartPlaceId, out var startPlace); TryGetPlaceById(tourInfo.DestinatePlaceId, out var destinatePlace); tourInfo.StartPlace = startPlace; tourInfo.DestinatePlace = destinatePlace; } pagination = new Pagination(total, page, pageSize > 0 ? pageSize : total); errorCode = ErrorCode.Success; } finally { DbService.DisconnectDb(ref _context); } return(errorCode); }
public bool GetUserLike(int userId, int postId, int page, int pageSize, out List <SimpleUser> simpleUsers, out Pagination pagination) { try { DbService.ConnectDb(out _context); var _ = _context.Posts.FirstOrDefault(t => t.Id == postId && t.DeletedAt == null) ?? throw new ExceptionWithMessage("Post not found"); var users = (from pl in _context.PostLikes join user in _context.Users on pl.UserId equals user.Id where pl.DeletedAt == null && pl.PostId == postId select user).ToList(); var total = users.Count(); var skip = pageSize * (page - 1); var canPage = skip < total; List <User> resultUsers; if (canPage) { resultUsers = pageSize <= 0 ? users : users .Skip(skip) .Take(pageSize) .ToList(); } else { resultUsers = new List <User>(); } simpleUsers = new List <SimpleUser>(); foreach (var user in resultUsers) { var friendType = _friendService.CalculateIsFriend(userId, user.Id); simpleUsers.Add(user.ToSimpleUser(friendType)); } pagination = new Pagination(total, page, pageSize > 0 ? pageSize : total); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public ErrorCode TryGetTours(int tourInfoId, int page, int pageSize, out List <Tour> tours, out Pagination pagination) { tours = null; pagination = null; ErrorCode errorCode; try { ValidatePageSize(ref page, ref pageSize); DbService.ConnectDb(out _context); var listTours = _context.Tours.Where(t => t.DeletedAt == null && t.TourInfoId == tourInfoId).ToList(); var total = listTours.Count(); var skip = pageSize * (page - 1); var canPage = skip < total; if (canPage) { tours = pageSize <= 0 ? listTours : listTours.Where(u => u.TourInfoId == tourInfoId) .Skip(skip) .Take(pageSize) .ToList(); } else { tours = new List <Tour>(); } foreach (var tour in tours) { TryGetTimelines(tour.Id, out var timeLines); TryGetTourInfoById(tour.TourInfoId, out var tourInfo); tour.TimeLines = timeLines; tour.TourInfo = tourInfo; } pagination = new Pagination(total, page, pageSize > 0 ? pageSize : total); errorCode = ErrorCode.Success; } finally { DbService.DisconnectDb(ref _context); } return(errorCode); }
public bool TryUpdatePlaceTypeById(PlaceType placeType) { try { DbService.ConnectDb(out _context); _ = _context.PlaceTypes.Update(placeType); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryUpdateTour(Tour tour) { try { DbService.ConnectDb(out _context); _context.Tours.Update(tour); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryUpdateUser(User user) { try { DbService.ConnectDb(out _context); _context.Users.Update(user); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryUpdatePlace(Place place) { try { DbService.ConnectDb(out _context); _context.Places.Update(place); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryRemovePlaceType(PlaceType placeType) { try { DbService.ConnectDb(out _context); placeType.Delete(); _context.SaveChanges(); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetTotalMember(int tourId, out int totalMember) { totalMember = 0; try { DbService.ConnectDb(out _context); totalMember = _context.TourMembers.Count(t => t.TourId == tourId); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetTourInfoById(int tourId, out TourInfo tourInfos) { tourInfos = null; try { DbService.ConnectDb(out _context); tourInfos = _context.TourInfos.FirstOrDefault(a => a.Id == tourId); } finally { DbService.DisconnectDb(ref _context); } return(true); }
public bool TryGetPlaceTypeById(int placeId, out PlaceType placeType) { placeType = null; try { DbService.ConnectDb(out _context); placeType = _context.PlaceTypes.FirstOrDefault(p => p.Id == placeId); } finally { DbService.DisconnectDb(ref _context); } return(true); }