Exemplo n.º 1
0
        public MdContract UpdateDto(MdContractVm.Dto instance)
        {
            MdContract result = null;
            var        item   = FindById(instance.Id);

            if (item != null)
            {
                result = Update(Mapper.Map <MdContractVm.Dto, MdContract>(instance));
                {//delete
                    var deleteList = UnitOfWork.Repository <MdDetailContract>().DbSet.Where(x => x.MaHd == result.MaHd).ToList();
                    deleteList.ForEach(x => x.ObjectState = ObjectState.Deleted);
                }
                {//insert data details
                    var merchandiseCollection = UnitOfWork.Repository <MdMerchandise>().DbSet;
                    var detail = Mapper.Map <List <MdContractVm.Detail>, List <MdDetailContract> >(instance.DataDetails);
                    detail.ForEach(x => {
                        var hang = merchandiseCollection.FirstOrDefault(u => u.MaVatTu == x.MaHang);
                        x.Id     = Guid.NewGuid().ToString();
                        x.MaHd   = result.MaHd;
                    });
                    UnitOfWork.Repository <MdDetailContract>().InsertRange(detail);
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public MdContract InsertDto(MdContractVm.Dto instance)
        {
            var result = Insert(Mapper.Map <MdContractVm.Dto, MdContract>(instance));
            var detail = Mapper.Map <List <MdContractVm.Detail>, List <MdDetailContract> >(instance.DataDetails);
            var merchandiseCollection = UnitOfWork.Repository <MdMerchandise>().DbSet;

            detail.ForEach(x => {
                var hang  = merchandiseCollection.FirstOrDefault(u => u.MaVatTu == x.MaHang);
                x.TenHang = hang != null ? hang.TenHang : "";
                x.Id      = Guid.NewGuid().ToString();
                x.MaHd    = result.MaHd;
            });
            UnitOfWork.Repository <MdDetailContract>().InsertRange(detail);
            return(result);
        }