public bool UpdateDept(DeptDetail model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .DeptDbSet .Single(e => e.DeptId == model.DeptId); entity.DeptName = model.DeptName; entity.PersonnelCount = model.PersonnelCount; return(ctx.SaveChanges() == 1); } }
public IHttpActionResult Put(DeptDetail dept) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateDeptService(); if (!service.UpdateDept(dept)) { return(InternalServerError()); } return(Ok()); }
public async Task <bool> UpdateDept(DateTime fromDate, DateTime toDate, string filter, string branchIds, Guid userId) { try { ///Nếu như !=0 tức là trên 1 tháng! var months = (toDate.Year * 12 + toDate.Month) - (fromDate.Year * 12 + fromDate.Month); if (months < 0) { throw new Exception("Should be ToDate than FromDate"); } else { ///Tính thời gian về cuối tháng DateTime ToDate_LastMonth = toDate; DateTime ToDate_ThisMonth = toDate; //loop for (var i = 0; i <= months; i++) { //nếu như khác giá trị ban đầu thì fromDate tăng lên 1 tháng if (i != 0) { fromDate = fromDate.AddMonths(1); ToDate_ThisMonth = new DateTime(fromDate.Year, fromDate.Month, 1); toDate = ToDate_ThisMonth.AddMonths(1).AddSeconds(-1);//ve cuoi thang nay } else { //Khi trường hợp nó bằng 0 tức là đang ở khoảng fromDate, cần phải đưa nó về cuối tháng //vụ đưa về cuối tháng mỗi thèng tự xử rùi không cần quan tâm làm gì? toDate = fromDate; } toDate = toDate.AddMonths(1).AddSeconds(-1);//ve cuoi thang nay var results = this.GetDeptList(fromDate, toDate, filter, branchIds, 0, 0); if (results != null && results.Count() > 0) { var endDate = fromDate.AddMonths(1); var month = endDate.Month; var year = endDate.Year; //get Dept var inv = this.appContext.Dept.FirstOrDefault( p => p.CreatedDate.Month == month && p.CreatedDate.Year == year && p.IsDeleted == false); //Initially ID Guid DeptId = Guid.NewGuid(); if (inv != null) { DeptId = inv.DeptId; } else { inv = new Dept { DeptId = DeptId, CreatedBy = userId, CreatedDate = endDate, IsDeleted = false, Note = endDate.ToString(), UpdatedBy = userId, UpdatedDate = DateTime.Now }; await this.appContext.AddAsync(inv); } var invds = this.appContext.DeptDetail.Where(p => p.DeptId == inv.DeptId); foreach (var item in results) { var itemExist = invds.FirstOrDefault(p => p.StudentId == item.StudentId); if (itemExist != null) { itemExist.Receipt = item.EndReceipt; itemExist.Payment = item.EndPayment; itemExist.UpdatedBy = userId; itemExist.UpdatedDate = DateTime.Now; } else { itemExist = new DeptDetail { DeptDetailId = Guid.NewGuid(), DeptId = DeptId, Receipt = item.EndReceipt, Payment = item.EndPayment, CreatedBy = userId, CreatedDate = DateTime.Now, StudentId = item.StudentId, IsDeleted = false, UpdatedBy = userId, UpdatedDate = DateTime.Now }; await this.appContext.AddAsync(itemExist); } } await this.appContext.SaveChangesAsync(); } } } return(true); } catch (Exception ex) { throw ex; } }