Пример #1
0
        public async Task <ActionResult <CartDetailsDTO> > AddCart(CartDTO cartDTO)
        {
            try
            {
                var cartNameExists   = _context.Carts.Any(c => c.CartName == cartDTO.CartName);
                var tagAddressExists = _context.Carts.Any(c => c.TagAddress == cartDTO.TagAddress);

                //check if CartName already exists
                if (cartNameExists)
                {
                    //add error message
                    ModelState.AddModelError("CartName", "CartName already exists.");
                }

                //check if TagAddress already exists
                if (tagAddressExists)
                {
                    //add error message
                    ModelState.AddModelError("TagAddress", "TagAddress already exists.");
                }

                //if model is not valid return error messages
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                }

                //create cart
                var cart = new Cart
                {
                    CartName   = cartDTO.CartName,
                    TagAddress = cartDTO.TagAddress
                };

                //insert cart
                _context.Carts.Add(cart);
                await _context.SaveChangesAsync();

                //return the new cart details
                return(CreatedAtAction(
                           nameof(GetCartByCartId),
                           new { cartId = cart.CartId },
                           CartToCartDetailsDTO(cart)));
            }
            catch (InvalidOperationException e)
            {
                //if exception is caught write to console and return error message
                Console.WriteLine("{0} Exception caught.", e);
                return(BadRequest(new { ApiProblem = "Invalid JSON format sent." }));
            }
        }
        public async Task <ActionResult <Site> > AddSite(SiteDTO siteDTO)
        {
            var site = new Site
            {
                SiteName = siteDTO.SiteName
            };

            _context.Sites.Add(site);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(
                       nameof(GetSiteById),
                       new { siteId = site.SiteId },
                       site));
        }
        public async Task <ActionResult <CartDetailsDTO> > AddCart(CartDTO cartDTO)
        {
            var cart = new Cart
            {
                CartName   = "x4JT" + DateTime.Now.Ticks.ToString("x"),
                TagAddress = cartDTO.TagAddress
            };

            _context.Carts.Add(cart);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(
                       nameof(GetCartByCartId),
                       new { cartId = cart.CartId },
                       CartToCartDetailsDTO(cart)));
        }
Пример #4
0
        public async Task <ActionResult <ProductDetailsDTO> > AddProductToCart(ProductDTO productDTO)
        {
            var product = new Product
            {
                LotId           = productDTO.LotId,
                ProductName     = productDTO.ProductName,
                ExpirationDate  = productDTO.ExpirationDate,
                Quantity        = productDTO.Quantity,
                VirtualSiteName = productDTO.VirtualSiteName,
                CartId          = productDTO.CartId
            };

            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(
                       nameof(GetProductById),
                       new { productId = product.ProductId },
                       ProductsToProductDetailsDTO(product)));
        }
Пример #5
0
        public async Task <ActionResult <SiteDetailsDTO> > AddSite(SiteDTO siteDTO)
        {
            try
            {
                var siteNameExists = _context.Sites.Any(s => s.SiteName == siteDTO.SiteName);

                //check if SiteName already exists
                if (siteNameExists)
                {
                    //add error message
                    ModelState.AddModelError("SiteName", "SiteName already exists.");
                }

                //try to parse given ref coordinates to npgsql box
                var coords = new NpgsqlBox();
                if (siteDTO.RefCoordinates != null)
                {
                    try
                    {
                        coords = NpgsqlBox.Parse(siteDTO.RefCoordinates);
                    }
                    catch (FormatException e)
                    {
                        //if exception is caught write to console and return error message
                        Console.WriteLine("{0} Exception caught.", e);
                        //add error message
                        ModelState.AddModelError("RefCoordinates", "Invalid input: RefCoordinates must be specified using the following syntax \'((x1,y1),(x2,y2))\' where (x1,y1) and (x2,y2) are any two opposite corners.");
                        return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                    }
                }

                if (ModelState.IsValid)
                {
                    var refCoordinatesExist = _context.Sites.Any(s => s.RefCoordinates.Equals(coords));

                    //check if RefCoordinates already exists
                    if (refCoordinatesExist)
                    {
                        //add error message
                        ModelState.AddModelError("RefCoordinates", "RefCoordinates already exist.");
                        return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                    }

                    //create site
                    var site = new Site
                    {
                        SiteName       = siteDTO.SiteName,
                        RefCoordinates = coords
                    };

                    //insert site
                    _context.Sites.Add(site);
                    await _context.SaveChangesAsync();

                    //return the new site details
                    return(CreatedAtAction(
                               nameof(GetSiteById),
                               new { siteId = site.SiteId },
                               SiteToSiteDetailsDTO(site)));
                }
                return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
            }
            catch (InvalidOperationException e)
            {
                //if exception is caught write to console and return error message
                Console.WriteLine("{0} Exception caught.", e);
                return(BadRequest(new { ApiProblem = "Invalid JSON format sent." }));
            }
        }
Пример #6
0
        public async Task <ActionResult <ProductDetailsDTO> > AddProductToCart(ProductDTO productDTO)
        {
            try
            {
                var cartIdExists  = _context.Carts.Any(c => c.CartId == productDTO.CartId);
                var productInCart = _context.Products.Any(p => p.LotId == productDTO.LotId);

                //check if cartid exists
                if (!cartIdExists && productDTO.CartId != null)
                {
                    //add error message
                    ModelState.AddModelError("CartId", "No cart found with given cart id.");
                }

                //check if product is already associated with another cart
                if (productInCart)
                {
                    //add error message
                    ModelState.AddModelError("LotId", "Product already added to a cart.");
                }

                //if model is not valid return error messages
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                }

                //find product with given lotid from MPROC's data
                var mPROCProduct = GetMPROCProductByLotId(productDTO.LotId);

                //if product does not exist in MPROC's data return error message
                if (mPROCProduct == null)
                {
                    //add error message
                    ModelState.AddModelError("LotId", "No product found with given lot id.");
                    return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                }

                //tries to parse expiration date to DateTime. if exception, expDate = null
                DateTime?expDate;
                try
                {
                    expDate = DateTime.Parse(mPROCProduct.USEBEFOREDATE);
                }
                catch (FormatException e)
                {
                    //if exception is caught write to console and expdate is null
                    Console.WriteLine("{0} Exception caught.", e);
                    expDate = null;
                }

                //create product
                var product = new Product
                {
                    LotId           = mPROCProduct.LOTID,
                    ProductName     = mPROCProduct.PRODUCTNAME,
                    ExpirationDate  = expDate,
                    Quantity        = mPROCProduct.COMPONENTQTY,
                    VirtualSiteName = mPROCProduct.STEPNAME,
                    CartId          = productDTO.CartId
                };

                //insert product
                _context.Products.Add(product);
                await _context.SaveChangesAsync();

                //return the created product details
                return(CreatedAtAction(
                           nameof(GetProductById),
                           new { productId = product.ProductId },
                           ProductsToProductDetailsDTO(product)));
            }
            catch (InvalidOperationException e)
            {
                //if exception is caught write to console and return error message
                Console.WriteLine("{0} Exception caught.", e);
                return(BadRequest(new { ApiProblem = "Invalid JSON format sent." }));
            }
        }
Пример #7
0
        public async Task <ActionResult <LocationHistoryDetailsDTO> > AddLocationToHistory(LocationHistoryDTO locationHistoryDTO)
        {
            try
            {
                var cartIdExists = _context.Carts.Any(c => c.CartId == locationHistoryDTO.CartId);
                var siteIdExists = _context.Sites.Any(s => s.SiteId == locationHistoryDTO.SiteId);

                //check if cartid exists
                if (!cartIdExists && locationHistoryDTO.CartId != null)
                {
                    //add error message
                    ModelState.AddModelError("CartId", "No cart found with given cart id.");
                }

                //check if siteid exists
                if (!siteIdExists && locationHistoryDTO.SiteId != null)
                {
                    //add error message
                    ModelState.AddModelError("SiteId", "No site found with given site id.");
                }

                //if model is not valid return error messages
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                }

                //tries to parse cart coordinates to NpgsqlPoint. if exception, return bad request
                var coords = new NpgsqlPoint();;
                try
                {
                    coords = NpgsqlPoint.Parse(locationHistoryDTO.CartCoordinates);
                }
                catch (FormatException e)
                {
                    //if exception is caught write to console and return error message
                    Console.WriteLine("{0} Exception caught.", e);
                    //add error message
                    ModelState.AddModelError("CartCoordinates", "Invalid input: CartCoordinates must be specified using the following syntax \'(x,y)\' where x and y are the respective coordinates, as floating-point numbers.");
                    return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray())));
                }

                //create location history
                var locationHistory = new LocationHistory
                {
                    CartId          = locationHistoryDTO.CartId,
                    SiteId          = locationHistoryDTO.SiteId,
                    CartCoordinates = coords,
                    RecordDate      = DateTime.Now
                };

                //insert location history
                _context.LocationHistories.Add(locationHistory);
                await _context.SaveChangesAsync();

                //rerturn the new location history details
                return(CreatedAtAction(
                           nameof(GetLocationHistoryByRecordId),
                           new { recordId = locationHistory.RecordId },
                           LocationHistoryToLocationHistoryDetailsDTO(locationHistory)));
            }
            catch (InvalidOperationException e)
            {
                //if exception is caught write to console and return error message
                Console.WriteLine("{0} Exception caught.", e);
                return(BadRequest(new { ApiProblem = "Invalid JSON format sent." }));
            }
        }