Exemplo n.º 1
0
        public async Task <ActionResult <BaseResponse> > Post(ListInputInfo listInput)
        {
            var list = listInput.listInputInfo;

            for (int i = 0; i < list.Count; i++)
            {
                if (string.IsNullOrEmpty(list[i].name) ||
                    string.IsNullOrEmpty(list[i].kind) ||
                    string.IsNullOrEmpty(list[i].author) ||
                    list[i].amount == 0)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.Empty
                    });
                }

                var exists = await _context.BOOKS
                             .Where(bo => convertToUnicode(bo.name) == convertToUnicode(list[i].name) &&
                                    bo.id != list[i].bookId).FirstOrDefaultAsync();

                if (exists != null)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.Existed_Book
                    });
                }
                // check policy
                if (list[i].amount < Roles.MinBookInput)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.NotEnoughMinStock,
                        Message = Roles.MinBookInput + ""
                    });
                }
                BOOK b = new BOOK();
                b.name      = list[i].name;
                b.kind      = list[i].kind;
                b.author    = list[i].author;
                b.price     = 0;
                b.stock     = list[i].amount;
                b.imageName = "";
                b.url       = "";
                b.isRemove  = false;
                _context.BOOKS.Add(b);
                _context.SaveChanges();

                INPUT input = new INPUT();
                input.stt      = list[i].stt;
                input.bookId   = list[i].bookId;
                input.amount   = list[i].amount;
                input.isRemove = false;
                _context.INPUTS.Add(input);
                _context.SaveChanges();
            }

            return(new BaseResponse {
                ErrorCode = Roles.Success
            });
        }
Exemplo n.º 2
0
        public async Task <ActionResult <BaseResponse> > Put(ListInputInfo listInput, int stt)
        {
            //var valid = await _context.INPUTS.Include(b => b.BOOK)
            //    .Where(x => x.isRemove == false && x.bookId == x.BOOK.id && x.stt == stt)
            //    .Select(s => new InputInfo
            //    {
            //        id = s.id,
            //        stt = s.stt,
            //        bookId = s.BOOK.id,
            //        name = s.BOOK.name,
            //        kind = s.BOOK.kind,
            //        author = s.BOOK.author,
            //        amount = s.amount,
            //        isRemove = s.isRemove
            //    }).ToListAsync();

            var list = listInput.listInputInfo;

            //if (valid.Count != list.Count)
            //{
            //    return new BaseResponse { ErrorCode = Roles.NotFound };
            //}

            for (int i = 0; i < list.Count; i++)
            {
                if (string.IsNullOrEmpty(list[i].name) ||
                    string.IsNullOrEmpty(list[i].kind) ||
                    string.IsNullOrEmpty(list[i].author) ||
                    list[i].amount == 0)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.Empty
                    });
                }

                var book = await _context.BOOKS
                           .Where(bo => convertToUnicode(bo.name) == convertToUnicode(list[i].name) &&
                                  bo.id != list[i].bookId).FirstOrDefaultAsync();

                if (book != null)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.Existed_Book
                    });
                }
                // check Roles
                book = await _context.BOOKS.FindAsync(list[i].bookId);

                if (book.stock > Roles.MaxBookStock)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.OverflowMaxStock,
                        Message = Roles.MaxBookStock + ""
                    });
                }
                else if (list[i].amount < Roles.MinBookInput)
                {
                    return(new BaseResponse
                    {
                        ErrorCode = Roles.NotEnoughMinStock,
                        Message = Roles.MinBookInput + ""
                    });
                }
                book.name   = list[i].name;
                book.kind   = list[i].kind;
                book.author = list[i].author;
                book.stock += list[i].amount;
                _context.BOOKS.Update(book);
                _context.SaveChanges();

                INPUT input = new INPUT();
                input.stt      = list[i].stt;
                input.bookId   = list[i].bookId;
                input.amount   = list[i].amount;
                input.isRemove = false;
                _context.INPUTS.Update(input);
                _context.SaveChanges();
            }

            return(new BaseResponse {
                ErrorCode = Roles.Success
            });
        }