public async Task <bool> CreateOffer(Offer offer, IEnumerable <IFormFile> offerPhotos) { if (offer == null) { return(false); } var currentUser = await profileService.GetCurrentUser(); if (currentUser == null) { return(false); } currentUser.Offers.Add(offer); offer.SetDetailsId(offer.OfferDetails.Id); offer.OfferDetails.SetOfferId(offer.Id); if (!await database.Complete()) { return(false); } if (offerPhotos != null) { var uploadedPhotos = await filesManager.Upload(offerPhotos, $"offers/{offer.Id}"); uploadedPhotos.ToList().ForEach(photo => offer.OfferPhotos.Add(OfferPhoto.Create <OfferPhoto>(photo.Path).SetOfferId(offer.Id))); } return(await database.Complete()); }
public async Task <IEnumerable <Story> > FetchStories() { var currentUser = await profileService.GetCurrentUser(); var friends = currentUser.FriendsSent.Concat(currentUser.FriendsReceived); return(currentUser.Stories.Where(s => s.DateExpires >= DateTime.Now).OrderByDescending(s => s.DateExpires) .Concat(await database.StoryRepository.GetUserCurrentStories(currentUser.Id)) .Distinct()); }
public async Task <Comment> CreateComment(string content, string postId) { var user = await profileService.GetCurrentUser(); var post = await database.PostRepository.FindById(postId) ?? throw new EntityNotFoundException("Post not found"); var comment = Comment.Create(content); user.Comments.Add(comment); post.Comments.Add(comment); return(await database.Complete() ? comment : null); }
public async Task <PagedList <Conversation> > GetConversations(GetConversationsPaginationRequest paginationRequest) { var sender = await profileService.GetCurrentUser(); var senderFriends = sender.FriendsSent.Concat(sender.FriendsReceived); var conversations = sender.MessagesSent.Concat(sender.MessagesReceived) .Where(m => string.IsNullOrEmpty(paginationRequest.Username) ? true : (m.SenderId == sender.Id ? m.Recipient.Username.ToLower().Contains(paginationRequest.Username.ToLower()) : m.Sender.Username.ToLower().Contains(paginationRequest.Username.ToLower()))) .OrderByDescending(m => m.DateSent) .GroupBy(m => new { m.SenderId, m.RecipientId }) .Select(g => { var message = g.First(); var conversation = new ConversationBuilder() .SentBy(message.SenderId) .SentTo(message.RecipientId) .SetLastMessage(new LastMessageBuilder() .SetText(message.Text) .SentBy(message.SenderId, message.Sender.Username, message.Sender.PhotoUrl) .Sent(message.DateSent) .MarkAsRead(message.IsRead) .Build()) .SetUserData(sender.Id == message.SenderId ? message.Recipient.Username : message.Sender.Username, sender.Id == message.SenderId ? message.Recipient.PhotoUrl : message.Sender.PhotoUrl) .Build(); return(conversation); }) .Where(c => senderFriends.Any(f => (f.SenderAccepted && f.RecipientAccepted) && (f.SenderId == c.SenderId || f.SenderId == c.RecipientId) && (f.RecipientId == c.SenderId || f.RecipientId == c.RecipientId))) .ToList(); var uniqueConversations = new List <Conversation>(); conversations.ForEach(c => { if (uniqueConversations.Count == 0 || !uniqueConversations.Any(uc => (uc.SenderId == c.SenderId && uc.RecipientId == c.RecipientId) || (uc.SenderId == c.RecipientId && uc.RecipientId == c.SenderId))) { uniqueConversations.Add(c); } }); return(uniqueConversations.ToPagedList <Conversation>(paginationRequest.PageNumber, paginationRequest.PageSize)); }
public async Task <bool> FollowOffer(string offerId) { var currentUser = await profileService.GetCurrentUser(); if (currentUser == null) { return(false); } var offer = await database.OfferRepository.Get(offerId); if (offer == null) { return(false); } var offerFollow = offer.OfferFollows.FirstOrDefault(of => of.UserId == currentUser.Id); if (offerFollow != null) { database.OfferFollowRepository.Delete(offerFollow); } else { offerFollow = OfferFollow.Create(offer.Id, currentUser.Id); database.OfferFollowRepository.Add(offerFollow); } return(await database.Complete()); }
public async Task <Opinion> CreateOpinion(string text, string offerId) { var currentUser = await profileService.GetCurrentUser(); if (currentUser == null) { return(null); } if (opinionValidationService.UserOpinionExists(currentUser, offerId)) { Alertify.Push("Your opinion on this offer already exists", AlertType.Warning); return(null); } var offer = await database.OfferRepository.Get(offerId); if (offer == null) { return(null); } if (offer.CreatorId == currentUser.Id) { Alertify.Push("You are not allowed to create opinion on your own offer", AlertType.Error); return(null); } var opinion = Opinion.Create(text); offer.Opinions.Add(opinion); currentUser.Opinions.Add(opinion); return(await database.Complete() ? opinion : null); }
public async Task <ToggleReportStatusResult> ToggleReportStatus(string reportId) { if (!rolesService.IsPermitted(await profileService.GetCurrentUser(), Constants.SupportRoles)) { throw new NoPermissionsException("You are not permitted to toggle this report status"); } var report = await GetReport(reportId); if (report.ReporterId == null) { return(null); } report.ToggleStatus(); return(await database.Complete() ? new ToggleReportStatusResult(report.IsClosed) : null); }
public async Task <GetProfileResponse> Handle(GetProfileRequest request, CancellationToken cancellationToken) { var userProfile = mapper.Map <UserProfileDto>(await profileServie.GetCurrentUser()); return(userProfile != null ? new GetProfileResponse { UserProfile = userProfile } : throw new EntityNotFoundException("User profile not found")); }
public async Task <BookedDate> BookDate(DateTime startDate, DateTime endDate, string offerId) { var offer = await database.OfferRepository.Get(offerId); if (offer == null) { return(null); } var currentUser = await profileService.GetCurrentUser(); if (currentUser.Id == offer.CreatorId) { Alertify.Push("You are owner of this offer", AlertType.Warning); return(null); } if (!bookingValidationService.IsBookingDateAvailable(startDate, endDate, offer)) { Alertify.Push("This date is already booked", AlertType.Warning); return(null); } if (currentUser != null && bookingValidationService.HasUserAnotherBookedDate(currentUser, offer.Id)) { Alertify.Push("You have already booked this offer", AlertType.Warning); return(null); } var bookedDate = BookedDate.Create(startDate, endDate); offer.BookedDates.Add(bookedDate); if (currentUser != null) { currentUser.BookedDates.Add(bookedDate); } return(await database.Complete() ? bookedDate : null); }
public async Task <Group> GetGroup(string groupId) { var group = await database.GroupRepository.FindById(groupId) ?? throw new EntityNotFoundException("Group not found"); var currentUser = await profileService.GetCurrentUser(); var currentUserGroups = currentUser.Groups.Concat(currentUser.GroupMembers.Where(m => m.IsAccepted).Select(m => m.Group)); return(GetGroupByMemberSpecification.Create(currentUser).IsSatisfied(group) ? group : throw new NoPermissionsException("You are not member of this group")); }
private async Task <User> GetCurrentAdmin(string userId) { var currentAdmin = await profileService.GetCurrentUser(); if (!currentAdmin.IsAdmin()) { throw new NoPermissionsException("You are not allowed to perform this action"); } if (currentAdmin.Id == userId) { throw new NoPermissionsException("You do not have permissions to manage your account"); } return(currentAdmin); }
public async Task <RefreshUserDataResponse> Handle(RefreshUserDataRequest request, CancellationToken cancellationToken) { var currentUser = await profileService.GetCurrentUser(); var token = await jwtAuthorizationTokenGenerator.GenerateToken(currentUser); if (currentUser != null && !string.IsNullOrEmpty(token)) { return new RefreshUserDataResponse { Token = token, User = mapper.Map <UserAuthDto>(currentUser) } } ; throw new AuthException("Some error occurred during signing in"); } }
public async Task <InviteMemberResult> InviteMember(string groupId, string userId) { var currentUser = await profileService.GetCurrentUser(); var group = await database.GroupRepository.FindById(groupId) ?? throw new EntityNotFoundException("Group not found"); if (currentUser.Id == userId) { throw new NoPermissionsException("You are not allowed to invite yourself to this group"); } if (!InviteMemberPermissionSmartEnum.FromValue((int)group.InviteMemberPermission) .ValidatePermission(currentUser.Id, group)) { throw new NoPermissionsException("You are not allowed to invite members in this group"); } if (IsUserGroupMemberSpecification.Create(userId).IsSatisfied(group)) { throw new DuplicateException("This user is currently member of this group"); } var member = GroupMember.Create(userId, groupId); var memberInvite = GroupInvite.Create(userId, groupId, isInvited: true); database.GroupMemberRepository.Add(member); group.GroupInvites.Add(memberInvite); if (await database.Complete()) { await notifier.Push(NotificationMessages.GroupInvitedNotification(group.Name), userId, NotificationType.GroupInvited); return(new InviteMemberResult(member, memberInvite)); } return(null); }