Example #1
0
        public async Task <IActionResult> PutMedicine(int id, Medicine medicine)
        {
            if (id != medicine.Id)
            {
                return(BadRequest());
            }

            _context.Entry(medicine).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MedicineExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutMedicine([FromRoute] int id, [FromBody] Medicine medicine)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != medicine.Id)
            {
                return(BadRequest());
            }

            var stopwatch = Stopwatch.StartNew();

            _context.Entry(medicine).State = EntityState.Modified;

            try
            {
                _logger.LogInformation($"Medicine with id# {id} is PUT. TimeElapsedInMilliSeconds: {stopwatch.ElapsedMilliseconds}");
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MedicineExists(id))
                {
                    _logger.LogError($"Medicine with id# {id} is not found. TimeElapsedInMilliSeconds: {stopwatch.ElapsedMilliseconds}");
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }
Example #3
0
        public async Task <IActionResult> PutMedicine([FromQuery] MedicineDTO medicineDTO)
        {
            var medicine = await _context.MedDbSet.FindAsync(medicineDTO.Id);

            if (medicine == null)
            {
                return(NoContent());
            }

            medicine.Notes = medicineDTO.Notes;
            _context.Entry(medicine).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MedicineExists(medicineDTO.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #4
0
        public async Task <bool> DeleteMedicine(Guid medicineID)
        {
            var medicine = await _medicineContext.Medicine.Where(p => p.Id == medicineID).FirstOrDefaultAsync();

            _medicineContext.Remove(medicine);
            await _medicineContext.SaveChangesAsync();

            return(true);
        }
Example #5
0
        public async Task <ActionResult <EventInfo> > AddNewEvent(EventInfo eventInfo)
        {
            await _context.EventInfo.AddAsync(eventInfo);

            await _context.SaveChangesAsync();

            return(Ok(eventInfo));
        }
        public async Task <ActionResult <MedicineInfo> > ResetMedicine(MedicineInfo medicineInfo)
        {
            var medicine = await _context.MedicineInfo.FindAsync(medicineInfo.MedicineId);

            medicine.UsedPortion = medicineInfo.UsedPortion;

            medicine.UsedPortion = 0;

            await _context.SaveChangesAsync();

            return(Ok(medicine.UsedPortion));
        }
        public static void Initialize(IServiceProvider serviceProvider)
        {
            var contextOptionsFromDi = serviceProvider.GetRequiredService <DbContextOptions <MedicineContext> >();

            using (var context = new MedicineContext(contextOptionsFromDi))
            {
                // check if data already present
                if (context.Medicines.Any())
                {
                    return;
                }

                // Seed Data if absent
                context.Medicines.AddRange(
                    new Medicine()
                {
                    Id         = 91,
                    Name       = "Med1",
                    Brand      = "brand1",
                    Price      = (decimal)29.12,
                    Quantity   = 10,
                    ExpiryDate = new DateTime(2020, 01, 01).Date,
                    Notes      = "medicine number 1"
                },
                    new Medicine()
                {
                    Id         = 92,
                    Name       = "Med2",
                    Brand      = "brand2",
                    Price      = (decimal)29.13,
                    Quantity   = 20,
                    ExpiryDate = new DateTime(2020, 01, 02).Date,
                    Notes      = "medicine number 2"
                });

                context.SaveChangesAsync();
            }
        }