public async Task <bool> StoreCodeBatch(CodeBatch codeBatch)
        {
            //setup HttpClient with content
            var httpClient = GetHttpClient();

            //setup body
            var content = new StreamContent(new MemoryStream(codeBatch.File));

            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");

            //construct full API endpoint uri
            DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
            var parameters          = new Dictionary <string, string>
            {
                { "Expiry", codeBatch.Expiry.ToString(dtfi) },
                { "EventName", codeBatch.EventName },
                { "Owner", codeBatch.Owner },
                { "AvaliableFrom", codeBatch.AvaliableFrom.ToString(dtfi) },
                { "AvaliableUntil", codeBatch.AvaliableUntil.ToString(dtfi) },
            };
            var apiBaseUrl = $"{_appSettings.APIBaseUrl}/codes";
            var apiUri     = QueryHelpers.AddQueryString(apiBaseUrl, parameters);

            //make request
            var responseMessage = await httpClient.PostAsync(apiUri, content);

            return(responseMessage.IsSuccessStatusCode);
        }
        private CodeBatch CastFormCollectionToCodeBatch(IFormCollection collection, IFormFile file)
        {
            //return null if there is no file attached
            if (file.Length == 0)
            {
                return(null);
            }

            byte[] fileBytes = null;
            using (var fileStream = file.OpenReadStream())
            {
                using (var ms = new MemoryStream())
                {
                    fileStream.CopyTo(ms);
                    fileBytes = ms.ToArray();
                }
            }

            var codeBatch = new CodeBatch()
            {
                Expiry         = Convert.ToDateTime(collection["Expiry"]),
                EventName      = collection["EventName"],
                Owner          = User.Identity.Name ?? "Anonymous",
                File           = fileBytes,
                AvaliableFrom  = Convert.ToDateTime(collection["AvaliableFrom"]),
                AvaliableUntil = Convert.ToDateTime(collection["AvaliableUntil"])
            };

            return(codeBatch);
        }
        public async Task <IActionResult> Post(CodeBatch codeBatch)
        {
            //check that the eventname is unique
            //get all codes
            var allCodes = await _storeRepository.GetCodes();

            //filter codes on EventName
            var codesInEvent = allCodes
                               .Where(x => x.EventName.ToLower() == codeBatch.EventName.ToLower())
                               .ToList();

            if (codesInEvent.Count > 0)
            {
                return(BadRequest("Event Name is not unique"));
            }

            // Get file into an array of strings
            byte[] file = Helpers.Helpers.ReadFileStream(Request.Body);
            codeBatch.File = file;
            var fileString = System.Text.Encoding.Default.GetString(codeBatch.File);

            string[] stringSeparators = new string[] { "\r\n" };
            string[] lines            = fileString.Split(stringSeparators, StringSplitOptions.None);

            // add each code to list
            var codesList = new List <Code>();

            foreach (var line in lines)
            {
                // split line to individual codes
                string[] promoCodes = line.Split(',');
                foreach (var promoCode in promoCodes)
                {
                    if (!string.IsNullOrEmpty(promoCode))
                    {
                        DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
                        var code = new Code()
                        {
                            PromoCode      = promoCode ?? string.Empty,
                            Claimed        = false,
                            EventName      = codeBatch.EventName ?? string.Empty,
                            Expiry         = Convert.ToDateTime(codeBatch.Expiry, dtfi),
                            Owner          = codeBatch.Owner ?? string.Empty,
                            AvaliableFrom  = Convert.ToDateTime(codeBatch.AvaliableFrom, dtfi),
                            AvaliableUntil = Convert.ToDateTime(codeBatch.AvaliableUntil, dtfi)
                        };

                        codesList.Add(code);
                    }
                }
            }

            // Store codes
            await _storeRepository.StoreCodes(codesList);

            return(Ok(codesList));
        }