コード例 #1
0
        public bool LoadToDb(IFormFile file)
        {
            //RequestRepository repository = new RequestRepository(_context);
            OrderService orderService = new OrderService(_context);

            var result = string.Empty;

            //read json file to string
            using (var reader = new StreamReader(file.OpenReadStream()))
            {
                result = reader.ReadToEnd();
            }

            RequestsJson rqst;

            //convert json to list of requests
            try
            {
                rqst = JsonConvert.DeserializeObject <RequestsJson>(result);
            }
            catch (JsonReaderException exception)
            {
                Debug.WriteLine(exception.Message);
                return(false);
            }

            if (rqst != null && rqst.Requests != null)
            {
                foreach (RequestJson rq in rqst.Requests)
                {
                    Request request = new Request
                    {
                        ClientId  = int.Parse(rq.ClientId ?? ""),
                        Name      = rq.Name ?? "",
                        Price     = Double.Parse(rq.Price ?? "", CultureInfo.InvariantCulture),
                        Quantity  = int.Parse(rq.Quantity ?? ""),
                        RequestId = long.Parse(rq.RequestId ?? "", CultureInfo.InvariantCulture)
                    };

                    var order = _context.Orders.Where(m => m.ClientId == request.ClientId &&
                                                      m.RequestId == request.RequestId);

                    if (order.Count() != 0)
                    {
                        request.Order = order.First();
                        order.First().Amount += request.Price * request.Quantity;
                    }
                    else
                    {
                        Order newOrder = new Order
                        {
                            ClientId  = request.ClientId,
                            RequestId = request.RequestId,
                            Amount    = request.Price * request.Quantity
                        };
                        _context.Add(newOrder);

                        request.Order = newOrder;
                    }

                    _context.Requests.Add(request);
                    _context.SaveChanges();
                }
                return(true);
            }

            return(false);
        }
コード例 #2
0
        public bool LoadToDb(IFormFile file)
        {
            //convert csv to list of requests
            using (var reader = new StreamReader(file.OpenReadStream()))
                using (var csv = new CsvReader(reader))
                {
                    csv.Configuration.Delimiter = ",";

                    List <RequestCsv> records = null;

                    try
                    {
                        records = csv.GetRecords <RequestCsv>().ToList();
                    }
                    catch (HeaderValidationException exception)
                    {
                        Debug.WriteLine(exception.Message);
                        return(false);
                    }
                    catch (Exception exception)
                    {
                        Debug.WriteLine(exception.Message);
                        return(false);
                    }

                    //add converted requests to database
                    if (records != null)
                    {
                        foreach (var rq in records)
                        {
                            Request request = new Request
                            {
                                ClientId  = int.Parse(rq.Client_Id),
                                Name      = rq.Name,
                                Price     = Double.Parse(rq.Price, CultureInfo.InvariantCulture),
                                Quantity  = int.Parse(rq.Quantity),
                                RequestId = long.Parse(rq.Request_id, CultureInfo.InvariantCulture)
                            };

                            var order = _context.Orders.Where(m => m.ClientId == request.ClientId &&
                                                              m.RequestId == request.RequestId);

                            if (order.Count() != 0)
                            {
                                request.Order = order.First();
                                order.First().Amount += request.Price * request.Quantity;
                            }
                            else
                            {
                                var newOrder = new Order
                                {
                                    ClientId  = request.ClientId,
                                    RequestId = request.RequestId,
                                    Amount    = request.Price * request.Quantity
                                };
                                _context.Orders.Add(newOrder);
                                _context.SaveChanges();
                                request.Order = newOrder;
                            }

                            _context.Requests.Add(request);
                            _context.SaveChanges();
                        }
                        return(true);
                    }

                    return(false);
                }
        }
コード例 #3
0
        public bool LoadToDb(IFormFile file)
        {
            //read json file to stream
            var reader = new StreamReader(file.OpenReadStream());


            //convert xml to list of requests
            XmlSerializer serializer = new XmlSerializer(typeof(RequestsXml));

            RequestsXml requests;

            try
            {
                requests = (RequestsXml)serializer.Deserialize(reader);
            }
            catch (Exception e)
            {
                return(false);
            }

            reader.Close();

            if (requests != null && requests.Requests != null)
            {
                foreach (RequestXml rq in requests.Requests)
                {
                    Request request = new Request
                    {
                        ClientId  = int.Parse(rq.ClientId),
                        Name      = rq.Name,
                        Price     = Double.Parse(rq.Price, CultureInfo.InvariantCulture),
                        Quantity  = int.Parse(rq.Quantity),
                        RequestId = long.Parse(rq.RequestId, CultureInfo.InvariantCulture)
                    };

                    var order = _context.Orders.Where(m => m.ClientId == request.ClientId &&
                                                      m.RequestId == request.RequestId);

                    if (order.Count() != 0)
                    {
                        request.Order = order.First();
                        order.First().Amount += request.Price * request.Quantity;
                    }
                    else
                    {
                        Order newOrder = new Order
                        {
                            ClientId  = request.ClientId,
                            RequestId = request.RequestId,
                            Amount    = request.Price * request.Quantity
                        };
                        _context.Orders.Add(newOrder);
                        _context.SaveChanges();

                        request.Order = newOrder;
                    }

                    _context.Requests.Add(request);
                    _context.SaveChanges();
                }
                return(true);
            }
            return(false);
        }