<commit_before>﻿namespace Merchello.Web.Editors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    using Merchello.Core;
    using Merchello.Core.Models;
    using Merchello.Core.Services;
    using Merchello.Web.Models.ContentEditing;
    using Merchello.Web.WebApi;

    using Umbraco.Web;
    using Umbraco.Web.Mvc;

    /// <summary>
    /// The invoice api controller.
    /// </summary>
    [PluginController("Merchello")]
    public class InvoiceApiController : MerchelloApiController
    {
        /// <summary>
        /// The _invoice service.
        /// </summary>
        private readonly IInvoiceService _invoiceService;

        /// <summary>
        /// Initializes a new instance of the <see cref="InvoiceApiController"/> class.
        /// </summary>
        public InvoiceApiController()
            : this(MerchelloContext.Current)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvoiceApiController"/> class.
        /// </summary>
        /// <param name="merchelloContext">
        /// The merchello context.
        /// </param>
        public InvoiceApiController(IMerchelloContext merchelloContext)
            : base((MerchelloContext) merchelloContext)
        {
            _invoiceService = merchelloContext.Services.InvoiceService;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvoiceApiController"/> class.
        /// </summary>
        /// <param name="merchelloContext">
        /// The merchello context.
        /// </param>
        /// <param name="umbracoContext">
        /// The umbraco context.
        /// </param>
        internal InvoiceApiController(IMerchelloContext merchelloContext, UmbracoContext umbracoContext)
            : base((MerchelloContext) merchelloContext, umbracoContext)
        {
            _invoiceService = merchelloContext.Services.InvoiceService;
        }

        /// <summary>
        /// Returns an Invoice by id (key)
        /// 
        /// GET /umbraco/Merchello/InvoiceApi/GetInvoice/{guid}
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="InvoiceDisplay"/>.
        /// </returns>
        public InvoiceDisplay GetInvoice(Guid id)
        {
            var invoice = _invoiceService.GetByKey(id) as Invoice;
            if (invoice == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return invoice.ToInvoiceDisplay();
        }

        /// <summary>
        /// Returns All Invoices
        /// 
        /// GET /umbraco/Merchello/InvoiceApi/GetInvoices
        /// </summary>
        /// <returns>
        /// The collection of all <see cref="InvoiceDisplay"/>.
        /// </returns>
        public QueryResultDisplay GetAllInvoices()
        {
            var invoices = InvoiceQuery.GetAllInvoices().ToArray();

            return new QueryResultDisplay()
            {
                Results = invoices,
                PageIndex = 0,
                TotalPages = 1,
                TotalResults = invoices.Count()
            };            
        }

        /// <summary>
        /// Returns All Invoices
        /// 
        /// GET /umbraco/Merchello/InvoiceApi/GetAllInvoices
        /// </summary>
        /// <param name="page">
        /// The page.
        /// </param>
        /// <param name="perPage">
        /// The per Page.
        /// </param>
        /// <returns>
        /// The paged collection of invoices.
        /// </returns>
        public QueryResultDisplay GetAllInvoices(int page, int perPage)
        {
            var allInvoices = InvoiceQuery.GetAllInvoices().ToArray();
            var invoices = allInvoices.Skip((page - 1) * perPage).Take(perPage);

            return new QueryResultDisplay()
            {
                Results = invoices,
                PageIndex = page,
                TotalPages = ((allInvoices.Count() - 1) / perPage) + 1,<commit_msg>Small bugfix to pagination of GetAllInvoices API call.<commit_after>﻿namespace Merchello.Web.Editors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    using Merchello.Core;
    using Merchello.Core.Models;
    using Merchello.Core.Services;
    using Merchello.Web.Models.ContentEditing;
    using Merchello.Web.WebApi;

    using Umbraco.Web;
    using Umbraco.Web.Mvc;

    /// <summary>
    /// The invoice api controller.
    /// </summary>
    [PluginController("Merchello")]
    public class InvoiceApiController : MerchelloApiController
    {
        /// <summary>
        /// The _invoice service.
        /// </summary>
        private readonly IInvoiceService _invoiceService;

        /// <summary>
        /// Initializes a new instance of the <see cref="InvoiceApiController"/> class.
        /// </summary>
        public InvoiceApiController()
            : this(MerchelloContext.Current)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvoiceApiController"/> class.
        /// </summary>
        /// <param name="merchelloContext">
        /// The merchello context.
        /// </param>
        public InvoiceApiController(IMerchelloContext merchelloContext)
            : base((MerchelloContext) merchelloContext)
        {
            _invoiceService = merchelloContext.Services.InvoiceService;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="InvoiceApiController"/> class.
        /// </summary>
        /// <param name="merchelloContext">
        /// The merchello context.
        /// </param>
        /// <param name="umbracoContext">
        /// The umbraco context.
        /// </param>
        internal InvoiceApiController(IMerchelloContext merchelloContext, UmbracoContext umbracoContext)
            : base((MerchelloContext) merchelloContext, umbracoContext)
        {
            _invoiceService = merchelloContext.Services.InvoiceService;
        }

        /// <summary>
        /// Returns an Invoice by id (key)
        /// 
        /// GET /umbraco/Merchello/InvoiceApi/GetInvoice/{guid}
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="InvoiceDisplay"/>.
        /// </returns>
        public InvoiceDisplay GetInvoice(Guid id)
        {
            var invoice = _invoiceService.GetByKey(id) as Invoice;
            if (invoice == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return invoice.ToInvoiceDisplay();
        }

        /// <summary>
        /// Returns All Invoices
        /// 
        /// GET /umbraco/Merchello/InvoiceApi/GetInvoices
        /// </summary>
        /// <returns>
        /// The collection of all <see cref="InvoiceDisplay"/>.
        /// </returns>
        public QueryResultDisplay GetAllInvoices()
        {
            var invoices = InvoiceQuery.GetAllInvoices().ToArray();

            return new QueryResultDisplay()
            {
                Results = invoices,
                PageIndex = 0,
                TotalPages = 1,
                TotalResults = invoices.Count()
            };            
        }

        /// <summary>
        /// Returns All Invoices
        /// 
        /// GET /umbraco/Merchello/InvoiceApi/GetAllInvoices
        /// </summary>
        /// <param name="page">
        /// The page.
        /// </param>
        /// <param name="perPage">
        /// The per Page.
        /// </param>
        /// <returns>
        /// The paged collection of invoices.
        /// </returns>
        public QueryResultDisplay GetAllInvoices(int page, int perPage)
        {
            var allInvoices = InvoiceQuery.GetAllInvoices().ToArray();
            var invoices = allInvoices.Skip(page * perPage).Take(perPage);

            return new QueryResultDisplay()
            {
                Results = invoices,
                PageIndex = page,
                TotalPages = ((allInvoices.Count() - 1) / perPage) + 1,
{'fraction_non_alphanumeric': 0.12275484715118354, 'fraction_numerical': 0.00107053645771381, 'mean_word_length': 1.587076923076923, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4257549', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2240, 'n_words': 483}