public async Task <ResultModel <string> > AddVendor(VendorPostVM model)
        {
            var result = new ResultModel <string>();

            //check if vendor already exists
            var check = await _context.Vendors
                        .Where(m => m.Name == model.Name.ToUpper())
                        .FirstOrDefaultAsync();

            if (check != null)
            {
                result.AddError("Vendor already exists with this name");
                return(result);
            }

            var vendor = new Vendor()
            {
                ValidPhoneNumber = model.ValidPhoneNumber,
                DateCreated      = DateTime.Now,
                Description      = model.Description,
                Name             = model.Name
            };

            if (model.ValidPhoneNumber == Enums.Enumerations.ValidPhoneNumberPattern.StartsWith)
            {
                //check pattern is valid. I.E list of comma separated numbers
                if (!CheckPatternIsValid(model.Pattern))
                {
                    result.AddError("Pattern should only contain comma separated numbers.");
                    return(result);
                }

                vendor.Pattern = model.Pattern;
            }

            _context.Vendors.Add(vendor);
            await _context.SaveChangesAsync();

            result.Data = "Saved Successfully";
            return(result);
        }
Example #2
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Id = id.GetValueOrDefault();
            var result = await _vendorService.GetVendorDetails(Id);

            if (result == null)
            {
                return(NotFound());
            }

            Vendor = new VendorPostVM()
            {
                Description      = result.Data.Description,
                Name             = result.Data.Name,
                Pattern          = result.Data.Pattern,
                ValidPhoneNumber = result.Data.ValidPhoneNumber,
            };
            return(Page());
        }
        public async Task <ResultModel <string> > UpdateVendor(int id, VendorPostVM model)
        {
            var result = new ResultModel <string>();

            //check if vendor already exists
            var vendor = await _context.Vendors
                         .Where(m => m.Id == id)
                         .FirstOrDefaultAsync();

            if (vendor == null)
            {
                result.AddError("Vendor not found.");
                return(result);
            }

            vendor.ValidPhoneNumber = model.ValidPhoneNumber;
            vendor.Description      = model.Description;
            vendor.Name             = model.Name;

            if (model.ValidPhoneNumber == Enums.Enumerations.ValidPhoneNumberPattern.StartsWith)
            {
                //check pattern is valid. I.E list of comma separated numbers
                if (!CheckPatternIsValid(model.Pattern))
                {
                    result.AddError("Pattern should only contain comma separated numbers.");
                    return(result);
                }

                vendor.Pattern = model.Pattern;
            }

            await _context.SaveChangesAsync();

            result.Data = "Updated Successfully";
            return(result);
        }