예제 #1
0
        public async Task <IActionResult> Put(int id, [FromBody] UpdateEmployeeDto item)
        {
            if (item == null || item.Id != id)
            {
                return(BadRequest());
            }

            try
            {
                var target = _context.Employees.FirstOrDefault(e => e.Id == id);

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

                var targetContact = _context.Contacts.FirstOrDefault(c => c.Id == target.ContactId);

                targetContact.FirstName       = item.FirstName;
                targetContact.LastName        = item.LastName;
                targetContact.Phone           = item.Phone;
                targetContact.MobilePhone     = item.MobilePhone;
                targetContact.Email           = item.Email;
                targetContact.OfficialEmail   = item.OfficialEmail;
                targetContact.PhysicalStreet  = item.Street;
                targetContact.PhysicalState   = item.State;
                targetContact.PhysicalCity    = item.City;
                targetContact.PhysicalZipCode = item.ZipCode;
                targetContact.PhysicalCountry = item.Country;

                target.EmployeeTypeId   = item.EmployeeTypeId;
                target.EmployeeStatusId = item.EmployeeStatusId;
                target.DesignationId    = item.DesignationId;
                target.IsUnderProbation = item.IsUnderProbation;
                target.EndedProbationAt = item.EndedProbationAt;
                target.HiredAt          = item.HiredAt;
                target.LeaveAt          = item.LeaveAt;
                target.ABAAccountName   = item.ABAAccountName;
                target.ABAAccountNumber = item.ABAAccountNumber;
                target.Remarks          = item.Remarks;

                _context.Update(targetContact);
                _context.Update(target);

                await _context.SaveChangesAsync();

                return(Ok());
            } catch (Exception e)
            {
                _logger.LogError("API_ERR | PUT_EMPLOYEE | item: {0} | ERR: {1}", JsonConvert.SerializeObject(item), e.StackTrace.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
예제 #2
0
        public async Task EditStickerColor(int stickerID, StickerColor color)
        {
            var sticker = dbContext.Stickers.Find(stickerID);

            if (sticker != null)
            {
                sticker.Color = color;
                dbContext.Update(sticker);
                await dbContext.SaveChangesAsync();

                _ = Clients.All.SendAsync("setStickerColor", sticker.StickerId, sticker.Color);
            }
        }
        public async Task <User> CreateOrUpdateAsync(User input)
        {
            input.CreateTime = DateTime.Now;
            input.Password   = input.Password.GetMd5();

            if (input.Id == 0)
            {
                var ise = await _dbContext.User.FirstOrDefaultAsync(u => u.LoginName == input.LoginName);

                if (ise != null)
                {
                    throw new StringResponseException("用户名已被占用了!");
                }

                var entity = await _dbContext.AddAsync(input);

                await _dbContext.SaveChangesAsync();

                return(entity.Entity);
            }

            if (input.LoginName == "admin")
            {
                throw new StringResponseException("这个可不能改哦!");
            }

            _dbContext.Update(input);
            await _dbContext.SaveChangesAsync();

            return(input);
        }
예제 #4
0
        public async Task <IActionResult> Update(int id, [FromBody] Student model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                model.Id = id;
                _context.Update(model);
                await _context.SaveChangesAsync();

                var data = new { error = 0, message = "Successfully Updated", obj = model };
                return(new JsonResult(data)
                {
                    StatusCode = StatusCodes.Status200OK
                });
            }
            catch (Exception ex)
            {
                var data = new { error = 1, message = ex.Message, trace = ex.StackTrace.ToString() };
                return(new JsonResult(data)
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }
예제 #5
0
        //针对情景二的异常的处理方案封装
        public void Method5()
        {
            const string userName = "******";

            SysUser user;

            using (var db = new DefaultDbContext())
            {
                user = db.Set <SysUser>().Single(m => m.UserName == userName);
            }
            user.AddDate = DateTime.Now;

            using (var db = new DefaultDbContext())
            {
                //通过查询,让上下文中存在相同主键的对象
                SysUser oldUser = db.Set <SysUser>().Find(user.Id);
                System.Console.WriteLine("更新前:{0}。", oldUser.AddDate);

                db.Update <SysUser>(user);
                int count = db.SaveChanges();
                System.Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                SysUser newUser = db.Set <SysUser>().Single(m => m.UserName == userName);
                System.Console.WriteLine("更新后:{0}。", newUser.AddDate);
            }
        }
예제 #6
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,IsDone")] ToDo toDo)
        {
            if (id != toDo.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    toDo.UserId = _userManager.GetUserId(HttpContext.User);
                    _context.Update(toDo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ToDoExists(toDo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(toDo));
        }
예제 #7
0
        public async Task <IActionResult> Put(int id, [FromBody] Contact model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            model.Id = id;
            _context.Update(model);
            await _context.SaveChangesAsync();

            return(Ok());
        }
예제 #8
0
        public async Task <Course> CreateOrUpdateAsync(Course input)
        {
            if (input.Id == 0)
            {
                var entity = await _dbContext.AddAsync(input);

                await _dbContext.SaveChangesAsync();

                return(entity.Entity);
            }

            _dbContext.Update(input);
            await _dbContext.SaveChangesAsync();

            return(input);
        }
예제 #9
0
        public ActionResult <Customer> Update(int id, [FromBody] UpdateCustomerCommand customer)
        {
            var entity = _defaultDbContext.Customers.FirstOrDefault(x => x.Id == id);

            if (entity is null)
            {
                return(NotFound());
            }

            entity.Update(customer.FirstName, customer.LastName, customer.Phone, customer.LastPurchase, customer.GenderId, customer.ClassificationId);
            entity.UpdateRegion(customer.RegionId, customer.StateId, customer.CityId);
            _defaultDbContext.Update(entity);
            _defaultDbContext.SaveChanges();

            return(Ok(entity));
        }
        public async Task <Honor> CreateOrUpdateAsync(Honor input)
        {
            input.CreateTime = DateTime.Now;

            if (input.Id == 0)
            {
                var entity = await _dbContext.AddAsync(input);

                await _dbContext.SaveChangesAsync();

                return(entity.Entity);
            }

            _dbContext.Update(input);
            await _dbContext.SaveChangesAsync();

            return(input);
        }
예제 #11
0
        //针对情景三的异常的处理方案封装
        public void Method9()
        {
            SysUser user = new SysUser {
                Id = 1, Password = "******" + DateTime.Now.Second
            };

            using (var db = new DefaultDbContext())
            {
                //先查询一次,让上下文中存在相同主键的对象
                SysUser oldUser = db.Set <SysUser>().Single(m => m.Id == 1);
                System.Console.WriteLine("更新前:{0}。", oldUser.Password);

                db.Update <SysUser>(m => new { m.Password }, user);
                int count = db.SaveChanges(false);
                System.Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                SysUser newUser = db.Set <SysUser>().Single(m => m.Id == 1);
                System.Console.WriteLine("更新后:{0}。", newUser.Password);
            }
        }
예제 #12
0
        public async Task <DataDictionary> CreateOrUpdateAsync(DataDictionary input)
        {
            if (input.Id == 0)
            {
                var ex = await _dbContext.DataDictionary.AnyAsync(d => !d.IsDeleted && d.Key == input.Key);

                if (ex)
                {
                    throw new StringResponseException("key 已存在!");
                }

                var entity = await _dbContext.AddAsync(input);

                await _dbContext.SaveChangesAsync();

                return(entity.Entity);
            }

            _dbContext.Update(input);
            await _dbContext.SaveChangesAsync();

            return(input);
        }
예제 #13
0
        public virtual int UpdatePosition(PositionInputDto PositionInputDto)
        {
            var Position = _mapper.Map <MbpPosition>(PositionInputDto);

            // 重新继承父级信息, todo优化 可以将此逻辑放到实体里面,当作领域逻辑
            var parentPosition = _defaultDbContext.MbpPositions.Where(m => m.Id == PositionInputDto.ParentId).FirstOrDefault();

            // 判断选择的上级是不是自己的下级岗位,这种选择是不合理的
            if (parentPosition.FullPositionName.StartsWith(Position.FullPositionName))
            {
                throw new Exception("不能使用当前下级岗位作为父级岗位");
            }

            // 刷新节点信息
            {
                // 刷新当前节点
                Position.SystemCode         = parentPosition.SystemCode;
                Position.FullPositionName   = string.Concat(parentPosition.FullPositionName, "/", Position.PositionName);
                Position.Level              = parentPosition.Level + 1;
                Position.ParentPositionCode = parentPosition.PositionCode;
                Position.ParentPositionName = parentPosition.PositionName;

                _defaultDbContext.Attach(Position);

                // 刷新下级节点
                var current = _defaultDbContext.MbpPositions.Include("ChildrenPosition.ChildrenPosition.ChildrenPosition.ChildrenPosition.ChildrenPosition")
                              .First(m => m.Id == Position.Id);
                RefreshChildrenInfo(Position, current.ChildrenPosition);

                _defaultDbContext.Attach(current);
            }

            _defaultDbContext.Update(Position);
            // 提交所有修改
            return(_defaultDbContext.SaveChanges());
        }
예제 #14
0
        public virtual int UpdateDept(DeptInputDto deptInputDto)
        {
            var dept = _mapper.Map <MbpDept>(deptInputDto);

            // 重新继承父级信息, todo优化 可以将此逻辑放到实体里面,当作领域逻辑
            var parentDept = _defaultDbContext.MbpDepts.Where(m => m.Id == deptInputDto.ParentId).FirstOrDefault();

            // 判断选择的上级是不是自己的下级部门,这种选择是不合理的
            if (parentDept.FullDeptName.StartsWith(dept.FullDeptName))
            {
                throw new Exception("不能使用当前下级部门作为父级部门");
            }

            // 刷新节点信息
            {
                // 刷新当前节点
                dept.SystemCode     = parentDept.SystemCode;
                dept.FullDeptName   = string.Concat(parentDept.FullDeptName, "/", dept.DeptName);
                dept.Level          = parentDept.Level + 1;
                dept.ParentDeptCode = parentDept.DeptCode;
                dept.ParentDeptName = parentDept.DeptName;

                _defaultDbContext.Attach(dept);

                // 刷新下级节点
                var current = _defaultDbContext.MbpDepts.Include("ChildrenDept.ChildrenDept.ChildrenDept.ChildrenDept.ChildrenDept")
                              .First(m => m.Id == dept.Id);
                RefreshChildrenInfo(dept, current.ChildrenDept);

                _defaultDbContext.Attach(current);
            }

            _defaultDbContext.Update(dept);
            // 提交所有修改
            return(_defaultDbContext.SaveChanges());
        }
 public virtual int Update(Expression <Func <T, bool> > @where, Expression <Func <T, T> > updateExp)
 {
     return(_dbContext.Update(where, updateExp));
 }