예제 #1
0
        private async Task <object> GetAll(IOwinContext context)
        {
            int skip      = 0;
            var skipParam = context.Request.Query.GetValues("skip")?.Single();

            if (skipParam != null)
            {
                skip = int.Parse(skipParam);
            }

            int take      = 20;
            var takeParam = context.Request.Query.GetValues("take")?.Single();

            if (takeParam != null)
            {
                take = int.Parse(takeParam);
            }

            var community = context.GetCommunity();
            var accounts  = await community.Accounts
                            .Where(acc => (acc.Flags & AccountFlags.Activated) > 0)
                            .OrderBy(acc => acc.Username)
                            .Skip(skip)
                            .Take(take)
                            .ToListAsync();

            return(accounts);
        }
예제 #2
0
        private async Task <object> Put(IOwinContext context)
        {
            // api/account/{id}
            var     session   = context.GetSession();
            var     community = context.GetCommunity();
            Account target    = await GetTargetAccount(context.Request.Path.Value, session, community);

            Account authorizer = await community.Accounts.GetBySteamIDAsync(session.SteamID);

            if (!IsChangeAuthorized(target, authorizer))
            {
                context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                return(false);
            }

            var form = await context.Request.ReadFormAsync();

            string fieldname = form["name"];
            string value     = form["value"];

            switch (fieldname)
            {
            case "username":
                if (string.IsNullOrWhiteSpace(value))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return("username is null or empty");
                }
                target.Username = value;
                await community.SaveChangesAsync();

                break;
            }
            return(true);
        }
예제 #3
0
        private async Task <object> GetSpecific(IOwinContext context, long id)
        {
            var community = context.GetCommunity();
            var account   = await community.Accounts.GetBySteamIDAsync(id);

            return(account);
        }
예제 #4
0
        private async Task <object> GetSpecific(IOwinContext context, long id)
        {
            var community   = context.GetCommunity();
            var techs       = community.Techs.Where(tech => tech.ID == id || tech.OwnerID == id);
            var transformed = await ApplyResponseTransform(context, techs);

            return(await transformed.ToListAsync());
        }
예제 #5
0
        private async Task <object> GetAll(IOwinContext context)
        {
            var community   = context.GetCommunity();
            var techs       = community.Techs.OrderByDescending(t => t.CreationTime);
            var transformed = await ApplyResponseTransform(context, techs);

            return(await transformed.ToListAsync());
        }
예제 #6
0
        private async Task <object> Post(IOwinContext context)
        {
            string contentType = context.Request.ContentType;

            if (!contentType.StartsWith("multipart/form-data;"))
            {
                return("error: not multipart form content type");
            }
            object titleValue;
            object fileValue;
            var    form = await MultipartFormParserasdf.ParseMultipartForm(contentType, context.Request.Body);

            if (!form.TryGetValue("title", out titleValue) || !(titleValue is string))
            {
                return("error: missing title");
            }
            if (!form.TryGetValue("file", out fileValue) || !(fileValue is FileUpload))
            {
                return("error: missing file");
            }

            var     session   = context.GetSession();
            var     community = context.GetCommunity();
            Account user      = await community.Accounts.GetBySessionAsync(session);

            if (user == null)
            {
                throw new InvalidOperationException("not logged in");
            }

            string     title = (string)titleValue;
            FileUpload file  = (FileUpload)fileValue;

            if (ConfigurationManager.AppSettings["Imgur.ClientID"] == null)
            {
                throw new InvalidOperationException("Imgur.API not configured");
            }

            var    client   = new ImgurClient(ConfigurationManager.AppSettings["Imgur.ClientID"], ConfigurationManager.AppSettings["Imgur.ClientSecret"]);
            var    endpoint = new ImageEndpoint(client);
            IImage image    = await endpoint.UploadImageBinaryAsync(file.Contents);

            Tech newTech = community.Techs.Create();

            newTech.Owner    = user;
            newTech.Title    = title;
            newTech.ImageUrl = image.Link;
            community.Techs.Add(newTech);
            await community.SaveChangesAsync();

            return(newTech.ID);
        }
예제 #7
0
        private async Task <object> Delete(IOwinContext context)
        {
            // api/account/{id}
            var args      = context.Request.Query;
            var session   = context.GetSession();
            var community = context.GetCommunity();

            Account target = await GetTargetAccount(context.Request.Path.Value, session, community);

            Account authorizer = await community.Accounts.GetBySteamIDAsync(session.SteamID);

            if (!IsChangeAuthorized(target, authorizer))
            {
                context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                return(false);
            }

            target.Flags &= ~(AccountFlags.Activated);
            await community.SaveChangesAsync();

            return(true);
        }
예제 #8
0
        private async Task HandleValidLogin(IOwinContext request, SteamID steamID)
        {
            var session = request.GetSession();

            session.SteamID = steamID;

            var     community = request.GetCommunity();
            long    steamID64 = steamID.ToSteamID64();
            Account account   = await community.Accounts.GetBySteamIDAsync(steamID64);

            if (account == null)
            {
                Console.WriteLine($"Creating account for: {steamID64}");
                account         = community.Accounts.Create();
                account.SteamID = steamID64;
                account.Flags   = AccountFlags.None;

                community.Accounts.Add(account);
                await community.SaveChangesAsync();
            }

            request.Response.Redirect(request.Request.Uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped));
        }
예제 #9
0
        private async Task <IQueryable> ApplyResponseTransform(IOwinContext context, IQueryable <Tech> query)
        {
            var session   = context.GetSession();
            var community = context.GetCommunity();

            if (session.IsValidLogin())
            {
                var accountID = session.SteamID.ToSteamID64();
                return(query.Select(t => new
                {
                    ID = t.ID,
                    Creator = t.Owner.Username,
                    CreatorID = t.OwnerID,
                    Title = t.Title,
                    ImageUrl = t.ImageUrl,
                    Featured = t.Featured,
                    CreationTime = t.CreationTime,
                    Subscriptions = community.Accounts.Count(acc => acc.SubscribedTechs.Contains(t)),
                    Subscribed = t.Subscribers.Any(acc => acc.SteamID == accountID),
                }));
            }
            else
            {
                return(query.Select(t => new
                {
                    ID = t.ID,
                    Creator = t.Owner.Username,
                    CreatorID = t.OwnerID,
                    Title = t.Title,
                    ImageUrl = t.ImageUrl,
                    Featured = t.Featured,
                    CreationTime = t.CreationTime,
                    Subscriptions = community.Accounts.Count(acc => acc.SubscribedTechs.Contains(t)),
                    Subscribed = false,
                }));
            }
        }