예제 #1
0
        public async Task <IActionResult> Post(
            [FromForm] BindWebsite website,
            [Required]
            [FromForm]
            [MaxFileSizeInMegabytes(2)]
            [OnlyJpegAndPngAllowed]
            IFormFile homepageSnapshot)
        {
            try
            {
                HomepageSnapshot newHomepageSnapshot = await _saveSnapshot.SaveAsync(homepageSnapshot);

                WebsiteCategory category = await _context.Categories.FirstOrDefaultAsync(cat => cat.Id == website.CategoryId);

                string encryptedPassword = _passwordSafe.Encrypt(website.LoginPassword);
                var    newWebsite        = new Website()
                {
                    Name             = website.Name,
                    URL              = website.URL,
                    Category         = category,
                    HomepageSnapshot = newHomepageSnapshot,
                    Login            = new WebsiteCredentials()
                    {
                        Email    = website.LoginEmail,
                        Password = encryptedPassword
                    }
                };

                _context.Websites.Add(newWebsite);
                await _context.SaveChangesAsync();

                return(Created(
                           UriHelper.BuildAbsolute(
                               Request.Scheme,
                               Request.Host,
                               Request.PathBase,
                               Request.Path.Add("/" + newWebsite.Id.ToString())),
                           OutputWebsite.FromWebsiteEntityWithFullImageUrl(
                               newWebsite,
                               Request.Scheme,
                               Request.Host,
                               _passwordSafe.Decrypt(newWebsite.Login.Password))));
            }catch (Exception ex)
            {
                //ToDo: log exception
                throw ex;
            }
        }
예제 #2
0
        public async Task <OutputWebsite> Get(int id)
        {
            Website website = await _context.Websites
                              .Include(w => w.Category)
                              .Include(w => w.HomepageSnapshot)
                              .Include(w => w.Login)
                              .FirstOrDefaultAsync(w => w.IsDeleted == false && w.Id == id);

            if (website == null)
            {
                throw new ArgumentOutOfRangeException("id");
            }

            return(OutputWebsite.FromWebsiteEntityWithFullImageUrl(
                       website,
                       Request.Scheme,
                       Request.Host,
                       _passwordSafe.Decrypt(website.Login.Password)));
        }
예제 #3
0
        public Pagination <OutputWebsite> Get(
            [FromQuery] string sortOrder,
            [FromQuery] string sortBy,
            [FromQuery] int page,
            [FromQuery] int resultsPerPage)
        {
            IQueryable <Website> websites = _context.Websites
                                            .Include(w => w.Category)
                                            .Include(w => w.HomepageSnapshot)
                                            .Include(w => w.Login)
                                            .Where(w => w.IsDeleted == false);

            //IQueryable<Website> filteredWebsites = null;

            if (resultsPerPage < 1)
            {
                resultsPerPage = 100;
            }
            else if (resultsPerPage > websites.Count())
            {
                resultsPerPage = websites.Count();
            }
            int maxPages    = (int)Math.Round((decimal)websites.Count() / resultsPerPage, MidpointRounding.ToPositiveInfinity);
            var currentPage = 1;

            if (page > maxPages)
            {
                currentPage = maxPages;
            }
            else if (page <= maxPages && page > 1)
            {
                currentPage = page;
            }
            //host/api/websites?page=1&resultsPerPage=${currentSetting}&sortBy=&sortOrder=


            if (!String.IsNullOrEmpty(sortBy))
            {
                switch (sortBy)
                {
                case "id":
                    websites = (sortOrder == "desc") ? websites.OrderByDescending(w => w.Id) : websites.OrderBy(w => w.Id);
                    break;

                case "name":
                    websites = (sortOrder == "desc") ? websites.OrderByDescending(w => w.Name) : websites.OrderBy(w => w.Name);
                    break;

                case "url":
                    websites = (sortOrder == "desc") ? websites.OrderByDescending(w => w.URL) : websites.OrderBy(w => w.URL);
                    break;
                }
            }
            websites = websites.Skip((currentPage - 1) * resultsPerPage).Take(resultsPerPage);

            IList <OutputWebsite> outputList = new List <OutputWebsite>();

            foreach (var website in websites)
            {
                outputList.Add(OutputWebsite.FromWebsiteEntityWithFullImageUrl(
                                   website,
                                   Request.Scheme,
                                   Request.Host,
                                   _passwordSafe.Decrypt(website.Login.Password)));
            }

            return(new Pagination <OutputWebsite>()
            {
                Data = outputList,
                ResultsPerPage = resultsPerPage,
                PageNumber = currentPage,
                PagesAvailable = maxPages,
                FirstPage = UriHelper.BuildAbsolute(
                    Request.Scheme,
                    Request.Host,
                    Request.PathBase,
                    Request.Path,
                    QueryString.Create(new KeyValuePair <string, string>[] {
                    new KeyValuePair <string, string>("page", "1"),
                    new KeyValuePair <string, string>("resultsPerPage", resultsPerPage.ToString()),
                    new KeyValuePair <string, string>("sortOrder", String.IsNullOrEmpty(sortOrder) ? "" : sortOrder.ToString()),
                    new KeyValuePair <string, string>("sortBy", String.IsNullOrEmpty(sortBy) ? "" : sortBy.ToString())
                })),
                LastPage = UriHelper.BuildAbsolute(
                    Request.Scheme,
                    Request.Host,
                    Request.PathBase,
                    Request.Path,
                    QueryString.Create(new KeyValuePair <string, string>[] {
                    new KeyValuePair <string, string>("page", maxPages.ToString()),
                    new KeyValuePair <string, string>("resultsPerPage", resultsPerPage.ToString()),
                    new KeyValuePair <string, string>("sortOrder", String.IsNullOrEmpty(sortOrder) ? "" : sortOrder.ToString()),
                    new KeyValuePair <string, string>("sortBy", String.IsNullOrEmpty(sortBy) ? "" : sortBy.ToString())
                })),
                PreviosPage = currentPage > 1 ? UriHelper.BuildAbsolute(
                    Request.Scheme,
                    Request.Host,
                    Request.PathBase,
                    Request.Path,
                    QueryString.Create(new KeyValuePair <string, string>[] {
                    new KeyValuePair <string, string>("page", (currentPage - 1).ToString()),
                    new KeyValuePair <string, string>("resultsPerPage", resultsPerPage.ToString()),
                    new KeyValuePair <string, string>("sortOrder", String.IsNullOrEmpty(sortOrder) ? "" : sortOrder.ToString()),
                    new KeyValuePair <string, string>("sortBy", String.IsNullOrEmpty(sortBy) ? "" : sortBy.ToString()),
                })) : null,
                NextPage = currentPage < maxPages?UriHelper.BuildAbsolute(
                    Request.Scheme,
                    Request.Host,
                    Request.PathBase,
                    Request.Path,
                    QueryString.Create(new KeyValuePair <string, string>[] {
                    new KeyValuePair <string, string>("page", (currentPage + 1).ToString()),
                    new KeyValuePair <string, string>("resultsPerPage", resultsPerPage.ToString()),
                    new KeyValuePair <string, string>("sortOrder", String.IsNullOrEmpty(sortOrder) ? "" : sortOrder.ToString()),
                    new KeyValuePair <string, string>("sortBy", String.IsNullOrEmpty(sortBy) ? "" : sortBy.ToString()),
                })) : null
            });
        }