示例#1
0
        public IActionResult Post([FromBody] CreateProductViewModel productViewModel)
        {
            try
            {
                productViewModel.Name = productViewModel.Name.Trim();

                if (_productRepository.List(p => p.Name.Equals(productViewModel.Name, StringComparison.OrdinalIgnoreCase)).Any())
                {
                    return(BadRequest("Duplicate product name!"));
                }

                var product = _mapper.Map <Product>(productViewModel);
                product.MaxOrderQuantity = 10;
                product.CreatedOn        = DateTime.Now;
                product.CreatedBy        = User.Identity.Name;

                _productRepository.Add(product);

                string qrCodeFileName = $"P{product.Id}_{Guid.NewGuid().ToString().Replace("-", "")}.png";
                string qrCodeContent  = "{\"objectType\":\"" + AssetClassifications.PRODUCT + "\",\"objectId\":" + product.Id + "}";

                product.QrCodeContent  = qrCodeContent;
                product.QrCodeImageUrl = QRCodeHelper.GenerateQrCodeImage(qrCodeFileName, qrCodeContent);

                _productRepository.Update(product);
                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
示例#2
0
        public IActionResult Post([FromBody] CreateDeviceViewModel deviceViewModel)
        {
            try
            {
                deviceViewModel.Name = deviceViewModel.Name.Trim();

                if (_deviceRepository.List(d => d.Name.Equals(deviceViewModel.Name, StringComparison.OrdinalIgnoreCase)).Any())
                {
                    return(BadRequest("Duplicate device name!"));
                }

                var device = _mapper.Map <Device>(deviceViewModel);

                device.IsDeleted  = false;
                device.IsBorrowed = false;
                device.CreatedOn  = DateTime.Now;
                device.CreatedBy  = User.Identity.Name;

                _deviceRepository.Add(device);

                string qrCodeFileName = $"P{device.Id}_{Guid.NewGuid().ToString().Replace("-", "")}.png";
                string qrCodeContent  = "{\"objectType\":\"" + AssetClassifications.DEVICE + "\",\"objectId\":" + device.Id + "}";

                device.QrCodeImageUrl = QRCodeHelper.GenerateQrCodeImage(qrCodeFileName, qrCodeContent);
                device.QrCodeContent  = qrCodeContent;

                _deviceRepository.Update(device);
                return(Ok());
            }
            catch (Exception EX)
            {
                return(StatusCode(500, EX.Message));
            }
        }