protected override async Task HandleInput(SubscriptionDeleteParams input) { using (var conenction = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(conenction); ISubscriptionRepo subRepo = database.GetRepo <ISubscriptionRepo>(conenction); //Pull in the space first Space?space = await spaceRepo.FindByName(input.Space); if (space == null) { throw new InvalidOperationException($"No space with name {input.Space} exists."); } //Try to pull in the subscription Subscription?sub = await subRepo.FindByUserAndSpace(input.User.Username, input.Space); if (sub == null) { throw new InvalidOperationException("Subscription does not exist"); } await subRepo.Delete(sub); space.SubscriptionCount--; await spaceRepo.Update(space); } }
protected override async Task <SpaceView?> HandleInput(FindByValueParams <string> input) { using (var connection = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); Space?s = await spaceRepo.FindByName(input.Value); return(s != null?spaceMapper.Map(s) : null); } }
public async Task<Subscription> CreateSubscription(SubscriptionCreate create, User user) { Space? space = await spaceRepo.FindByName(create.Space); if (space == null) { throw new NotFoundException($"No space with name {create.Space} found."); } Subscription s = factory.CreateFor(user, space); await repo.Add(s); await bus.Dispatch(new SubscriptionCreateEvent(s)); return s; }
protected override async Task <PostView?> HandleInput(PostCreateParams input) { using (var connection = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); Space?space = await spaceRepo.FindByName(input.Space); if (space == null) { throw new InvalidOperationException($"No space with name ${input.Space} found."); } using (var transaction = connection.BeginTransaction()) { Post post = new Post() { Type = input.Type, Title = input.Title, Body = input.Body, User = input.User, CreationDate = DateTime.UtcNow, Space = space }; if (post.Type == PostType.Link && !System.Text.RegularExpressions.Regex.IsMatch(post.Body, Regex.UrlProtocol)) { post.Body = $"http://{post.Body}"; } // Not liking these count caches. Makes no sense? post.Upvotes++; await postRepo.Add(post); Vote upvote = new Vote() { User = input.User, ResourceId = post.Id, ResourceType = VoteResourceType.Post, Direction = VoteDirection.Up }; await voteRepo.Add(upvote); post.Vote = upvote; transaction.Commit(); return(postMapper.Map(post)); } } }
protected override async Task <SubscriptionView> HandleInput(SubscriptionCreateParams input) { using (var connection = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); ISubscriptionRepo subRepo = database.GetRepo <ISubscriptionRepo>(connection); //Check to see if the Space exists first Space?space = await spaceRepo.FindByName(input.Space); if (space == null) { throw new InvalidOperationException($"No space with name {input.Space} exists."); } //Ensure no sub for this combo is already in place Subscription?existingSub = await subRepo.FindByUserAndSpace(input.User.Username, input.Space); if (existingSub != null) { return(subscriptionMapper.Map(existingSub)); } //Create the subscription Subscription sub = new Subscription() { Space = space, User = input.User }; await subRepo.Add(sub); //Update sub count space.SubscriptionCount++; await spaceRepo.Update(space); return(subscriptionMapper.Map(sub)); } }
protected override async Task <SpaceView> HandleInput(SpaceUpdateParams input) { using (var connection = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); Space?s = await spaceRepo.FindByName(input.Name); if (s == null) { throw new InvalidOperationException(); } if (!(await this.spacePermissionHandler.HasPermission(input.User, PermissionAction.UpdateSpace, s))) { throw new AuthorizationException(); } s.Description = input.Description; await spaceRepo.Update(s); return(spaceMapper.Map(s)); } }
protected override async Task <SpaceView> HandleInput(SpaceCreateParams input) { using (var connection = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); Space? existing = await spaceRepo.FindByName(input.Name); if (existing != null) { throw new InvalidOperationException($"Space name {input.Name} is already taken."); } Space s = new Space() { Name = input.Name, Description = input.Description, User = input.User, CreationDate = DateTime.UtcNow }; await spaceRepo.Add(s); return(spaceMapper.Map(s)); } }
public async Task <Space?> FindByName(string name) => await repo.FindByName(name);