Exemplo n.º 1
0
        public async Task <ActionResult> UpdateDrugAndFertilizer(int id, [FromBody] DrugAndFeltilizerDto data)
        {
            if (id != data.ID)
            {
                return(BadRequest());
            }

            var editeddata = await _context.Products.FirstOrDefaultAsync(s => s.Id == id);

            editeddata.Name           = data.Name;
            editeddata.MainIngredient = await _context.MainIngredients.FirstOrDefaultAsync(w => w.Id == data.MainIngredient);

            editeddata.MeasurementUnit = await _context.MeasurementUnits.FirstOrDefaultAsync(w => w.Id == data.MeasurementUnit);

            editeddata.FertilizerKind = await _context.FertilizerKind.FirstOrDefaultAsync(w => w.Id == data.Category);

            try
            {
                _context.Entry(editeddata).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> AddDrugAndFeltilizer([FromBody] DrugAndFeltilizerDto data)
        {
            int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var logineduser = await _auth.VerifyUser(id);

            var _feltizerKind = await _context.FertilizerKind.FirstOrDefaultAsync(w => w.Id == data.Category);

            if (_feltizerKind == null)
            {
                return(BadRequest("Bele bir kategorya yoxdur"));
            }

            var _mainIngredient = await _context.MainIngredients.FirstOrDefaultAsync(s => s.Id == data.MainIngredient);

            if (_mainIngredient == null)
            {
                return(BadRequest("Bele bir tesir edici madde yoxdur"));
            }
            var _measurementUnit = await _context.MeasurementUnits.FirstOrDefaultAsync(s => s.Id == data.MeasurementUnit);

            if (_measurementUnit == null)
            {
                return(BadRequest("Bele bir kategorya yoxdur"));
            }
            Product _product = new Product()
            {
                Name            = data.Name,
                FertilizerKind  = _feltizerKind,
                MainIngredient  = _mainIngredient,
                MeasurementUnit = _measurementUnit,
                Company         = logineduser.Company,
                Status          = true
            };

            try
            {
                await _context.Products.AddAsync(_product);

                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }

            return(StatusCode(201));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> GetAllDrugAndFeltilizer(string lang, int id)
        {
            int userid = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var logineduser = await _auth.VerifyUser(userid);

            DrugAndFeltilizerDto data = await _context.Products.Select(s => new DrugAndFeltilizerDto()
            {
                ID              = s.Id,
                Name            = s.Name,
                Category        = s.FertilizerKind.Id,
                MainIngredient  = s.MainIngredient.Id,
                MeasurementUnit = s.MeasurementUnit.Id
            }).FirstOrDefaultAsync(w => w.ID == id);

            return(Ok(data));
        }