public async Task <PokemonRaidPost> AddOrUpdatePost(PokemonRaidPost post)
        {
            var loc            = Uri.UnescapeDataString(post.FullLocation);
            var locationEntity = await Locations.SingleOrDefaultAsync(x => x.ServerId == post.GuildId && x.Name == loc);

            if (locationEntity == null)
            {
                var newLoc = _mapper.Map <RaidPostLocationEntity>(post);
                locationEntity = Locations.Add(newLoc).Entity;
                await SaveChangesAsync();
            }

            var bossEntity = await Pokemon.SingleOrDefaultAsync(x => x.Id == post.PokemonId);

            if (bossEntity == null)
            {
                var newBoss = _mapper.Map <PokemonEntity>(post);
                bossEntity = Pokemon.Add(newBoss).Entity;
            }

            var postEntity = new RaidPostEntity();

            if (post.DbId != default(ulong))
            {
                postEntity = await Posts.FirstOrDefaultAsync(x => x.Id == post.DbId);
            }

            _mapper.Map(post, postEntity);

            postEntity.Pokemon   = bossEntity;
            postEntity.PokemonId = bossEntity.Id;

            postEntity.Location   = locationEntity;
            postEntity.LocationId = locationEntity.Id;

            if (post.DbId == default(ulong))
            {
                Add(postEntity);
                await SaveChangesAsync();

                post.DbId = postEntity.Id;
            }

            foreach (var channelId in post.ChannelMessages.Keys.Where(x => ChannelPosts.Where(y => y.ChannelId == x && y.RaidPostId == post.DbId).Count() == 0))
            {
                var channelPost = new RaidPostChannelEntity()
                {
                    ChannelId = channelId, RaidPostId = post.DbId
                };
                Add(channelPost);
            }

            await SaveChangesAsync();

            post.DbLocationId = locationEntity.Id;
            return(post);
        }
Ejemplo n.º 2
0
        public static async Task <APIResponse <ChannelPosts> > GetPosts(string channel, int offset, int limit)
        {
            if (string.IsNullOrWhiteSpace(Token))
            {
                return new APIResponse <ChannelPosts>()
                       {
                           Success = false, Error = "Not logged in"
                       }
            }
            ;

            APIResponse <JObject> response = await MakeAPIRequest <JObject>(string.Format("channels/{0}/posts/{1}/{2}", channel, offset, limit));

            if (!response.Success)
            {
                return new APIResponse <ChannelPosts> {
                           Success = false, Error = response.Error
                }
            }
            ;

            ChannelPosts posts = new ChannelPosts()
            {
                Order = response.Value["order"].ToObject <List <string> >(), Posts = new List <Post>()
            };

            foreach (JProperty post in response.Value["posts"])
            {
                Post newPost = post.Last.ToObject <Post>();

                if (newPost.Type != PostType.Default)
                {
                    newPost.User = User.System;
                }

                posts.Posts.Add(newPost);
            }

            posts.Order.Reverse();

            return(new APIResponse <ChannelPosts>()
            {
                Success = true, Value = posts
            });
        }