示例#1
0
        public async Task <ActionResult <Tinplate> > PostTinplate(Tinplate tinplate)
        {
            //check if there is a tinplate with the same batch number already exist
            if (TinplateExists(tinplate.Id))
            {
                return(BadRequest());
            }

            if (tinplate.CanType == CanType._502)
            {
                tinplate.TinplateQty = tinplate.TinplateQty * 10 + tinplate.Good;
            }
            else if (tinplate.CanType == CanType._401)
            {
                tinplate.TinplateQty = tinplate.TinplateQty * 18 + tinplate.Good;
            }

            //log the activities history
            var currentUser = "******";

            tinplate.UpdateTime = DateTime.Now;
            var newNote = $"On {tinplate.UpdateTime}, this pallet of tinplate was accepted by {currentUser}.\n";

            tinplate.Note += newNote;

            _context.Tinplates.Add(tinplate);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetTinplate), new { id = tinplate.Id }, tinplate));
        }
示例#2
0
        public async Task <IActionResult> PutTinplate(long id, TinplateStatus newStatus)
        {
            Tinplate tinplate = _context.Tinplates.Find(id);

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

            //prevent double Production/Consumed
            if (tinplate.TinplateStatus >= newStatus)
            {
                return(BadRequest());
            }
            var currentUser = "******";

            tinplate.UpdateTime = DateTime.Now;

            if (newStatus == TinplateStatus.Production)
            {
                //check if there are any Tinplate in plant
                if (_context.Tinplates.Any(t => t.TinplateStatus == newStatus))
                {
                    Tinplate prevTinplate = _context.Tinplates.FirstOrDefault(t => t.TinplateStatus == newStatus);
                    prevTinplate.TinplateStatus = TinplateStatus.Consumed;

                    var preNewNote = $"On {tinplate.UpdateTime}, this pallet of tinplate has been used, operator: {currentUser}.\n";
                    prevTinplate.Note += preNewNote;

                    _context.Entry(prevTinplate).State = EntityState.Modified;
                }

                tinplate.TinplateStatus = TinplateStatus.Production;
                //log the activities history
                var newNote = $"On {tinplate.UpdateTime}, this pallet of tinplate has been using, operator: {currentUser}.\n";
                tinplate.Note += newNote;
            }
            else if (newStatus == TinplateStatus.Consumed)
            {
                tinplate.TinplateStatus = TinplateStatus.Consumed;
                //log the activities history
                var newNote = $"On {tinplate.UpdateTime}, this pallet of tinplate has been used, operator: {currentUser}.\n";
                tinplate.Note += newNote;
            }

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

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

            return(NoContent());
        }