public async Task <Category> AddCategory(Category category) { using (var transaction = _sqlServerContext.Database.BeginTransaction()) { try { var entity = await _sqlServerContext.Categories.FirstOrDefaultAsync(item => item.CategoryName == category.CategoryName); if (entity == null) { await _sqlServerContext.Categories.AddRangeAsync(category); await _sqlServerContext.SaveChangesAsync(); } else { category.CategoryId = 0; category.CategoryName = ""; category.IsActive = false; category.CreatedDate = DateTime.Now; category.CreatedBy = 0; category.UpdatedBy = 0; category.UpdatedDate = DateTime.Now; } transaction.Commit(); return(category); } catch (Exception exception) { transaction.Rollback(); throw exception; } } }
/// <summary> /// 添加服务 /// </summary> /// <param name="service">服务信息</param> /// <returns>写入基础数据库的状态项数</returns> public async Task <HttpResponseMessage> AddService(Service service) { _db.Service.Add(service); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, Json(result))); }
public async Task <IActionResult> PutEmpleado(long id, Empleado empleado) { if (id != empleado.Id) { return(BadRequest()); } _context.Entry(empleado).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmpleadoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <HttpResponseMessage> AddUser(User user) { HttpResponseMessage response; if (!ModelState.IsValid) { response = Request.CreateResponse(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(ModelState.ExpendErrors())); } else { if (user.CreatedBy == null || user.ModifiedBy == null) { user.UserId = Guid.NewGuid(); user.CreatedBy = user.UserId; user.ModifiedBy = user.UserId; } user.Token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user.UserName}:{user.Password}")); user.Password = Base64Helper.EncodeBase64(user.Password); _db.User.Add(user); await _db.SaveChangesAsync(); var result = new { UserId = user.UserId }; response = Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(result)); } return(response); }
public async Task <Unit> AddUnit(Unit unit) { using (var transaction = _sqlServerContext.Database.BeginTransaction()) { try { var entity = await _sqlServerContext.Units.FirstOrDefaultAsync(item => item.UnitName == unit.UnitName); if (entity == null) { await _sqlServerContext.Units.AddRangeAsync(unit); await _sqlServerContext.SaveChangesAsync(); } else { unit.UnitId = 0; unit.UnitName = ""; unit.IsActive = false; unit.CreatedDate = DateTime.Now; unit.CreatedBy = 0; unit.UpdatedBy = 0; unit.UpdatedDate = DateTime.Now; } transaction.Commit(); return(unit); } catch (Exception exception) { transaction.Rollback(); throw exception; } } }
public async Task <IActionResult> PutUsuario(int id, Usuario usuario) { if (id != usuario.Id) { return(BadRequest()); } _context.Entry(usuario).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UsuarioExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCategoria(int id, Categoria categoria) { if (id != categoria.id_categoria) { return(BadRequest()); } _context.Entry(categoria).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoriaExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <CustomerDTO> CustomerDelete(CustomerDTO customerDTO) { var dataObejct = customerDTO as CustomerDTO; _sqlServerContext.Remove(customerDTO); await _sqlServerContext.SaveChangesAsync(); return(customerDTO); }
public async Task <ProductDTO> ProductDelete(ProductDTO productDTO) { var dataObject = productDTO as Product; _sqlServerContext.Remove <Product>(dataObject); await _sqlServerContext.SaveChangesAsync(); return(productDTO); }
/// <summary> /// 添加用户组 /// </summary> /// <param name="id">创建人用户编号</param> /// <param name="userGroup">用户组信息</param> /// <returns>写入基础数据库的状态项数</returns> public async Task <HttpResponseMessage> AddUserGroup(int id, UserGroup userGroup) { userGroup.CreateByUserId = id; userGroup.CreateTime = DateTime.Now; _db.UserGroup.Add(userGroup); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, Json(result))); }
public async Task <EmployeeDTO> EmployeeDelete(EmployeeDTO employeeDTO) { var dataObject = employeeDTO as Employee; _sqlServerContext.Remove <Employee>(dataObject); await _sqlServerContext.SaveChangesAsync(); return(employeeDTO); }
public async Task <ActionResult <Product> > Create([FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _productsDbContext.Products.AddAsync(product); await _productsDbContext.SaveChangesAsync(); return(Ok(product)); }
/// <summary> /// 由用户组移入角色 /// </summary> /// <param name="id">用户组编号</param> /// <param name="roleId">角色编号</param> /// <returns>写入基础数据库的状态项数</returns> public async Task <HttpResponseMessage> MoveInRoleByUserGroup(int id, int roleId) { var userGroupRoleMapping = new UserGroupRoleMapping { UserGroupId = id, RoleId = roleId }; _db.UserGroupRoleMapping.Add(userGroupRoleMapping); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, Json(result))); }
/// <summary> /// 由角色移入权限 /// </summary> /// <param name="id">角色编号</param> /// <param name="permissionId">权限编号</param> /// <returns>写入基础数据库的状态项数</returns> public async Task <HttpResponseMessage> MoveInPermissionByRole(int id, int permissionId) { var rolePermissionMapping = new RolePermissionMapping { RoleId = id, PermissionId = permissionId }; _db.RolePermissionMapping.Add(rolePermissionMapping); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, Json(result))); }
public async Task <ActionResult <Risoluzione> > AddRisoluzione(Risoluzione risoluzione) { if (!ModelState.IsValid) { return(BadRequest()); } var utente = await _context.Utenti.FindAsync(risoluzione.IdGiocatore); if (utente == null) { return(BadRequest()); } var personaggio = await _context.Personaggi.FindAsync(risoluzione.IdPersonaggio); if (personaggio == null) { return(BadRequest()); } _context.Risoluzioni.Add(risoluzione); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetRisoluzioniUtente), new { username = utente.Username }, risoluzione)); }
public async Task <ActionResult> UpdateLivello(long IdUtente) { var statoLivello = await _context.StatiLivelli.Where(stato => stato.IdUtente == IdUtente).FirstOrDefaultAsync(); if (statoLivello == null) { return(NotFound()); } statoLivello.IdLivello = statoLivello.IdLivello + 1; _context.StatiLivelli.Update(statoLivello); await _context.SaveChangesAsync(); return(NoContent()); }
/// <summary> ///操作类型 /// </summary> /// <param name="operationType">操作类型</param> /// <returns>写入基础数据库的状态项数</returns> public async Task <HttpResponseMessage> AddOperationType(OperationType operationType) { _db.OperationType.Add(operationType); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, Json(result))); }
public async Task <TestModel> Save(TestModel testModel) { await _sqlServerContext.TestModel.AddAsync(testModel); await _sqlServerContext.SaveChangesAsync(); return(null); }
/// <summary> /// 添加操作日志 /// </summary> /// <param name="operationLog">服务信息</param> /// <returns>写入基础数据库的状态项数</returns> public async Task <HttpResponseMessage> AddService(OperationLog operationLog) { operationLog.CreateTime = DateTime.Now; _db.OperationLog.Add(operationLog); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, Json(result))); }
public async Task <Product> AddProduct(Product product) { using (var transaction = _sqlServerContext.Database.BeginTransaction()) { try { var entity = await _sqlServerContext.Products.FirstOrDefaultAsync(item => item.ProductName == product.ProductName); if (entity == null) { await _sqlServerContext.AddRangeAsync(product); await _sqlServerContext.SaveChangesAsync(); } else { product.ProductId = 0; product.CategoryId = 0; product.ProductName = ""; product.ProductCode = ""; product.SerialNo = ""; product.TypeId = 0; product.UnitId = 0; product.BrandId = 0; product.ImageURL = ""; product.Cost = 0; product.Price = 0; product.Quantity = 0; product.IsActive = false; product.CreatedDate = DateTime.Now; product.CreatedBy = 0; product.UpdatedBy = 0; product.UpdatedDate = DateTime.Now; } transaction.Commit(); return(product); } catch (Exception exception) { transaction.Rollback(); throw exception; } } }
public async Task <Supplier> AddSupplier(Supplier supplier) { using (var transaction = _sqlServerContext.Database.BeginTransaction()) { try { var entity = await _sqlServerContext.Suppliers.FirstOrDefaultAsync(item => item.Phone == supplier.Phone); if (entity == null) { await _sqlServerContext.AddRangeAsync(supplier); await _sqlServerContext.SaveChangesAsync(); } else { supplier.SupplierId = 0; supplier.SupplierName = ""; supplier.CompanyId = 0; supplier.CountryId = 0; supplier.PostalCode = ""; supplier.VatNumber = ""; supplier.Email = ""; supplier.Phone = ""; supplier.Address = ""; supplier.City = ""; supplier.State = ""; supplier.IsActive = false; supplier.CreatedDate = DateTime.Now; supplier.CreatedBy = 0; supplier.UpdatedBy = 0; supplier.UpdatedDate = DateTime.Now; } transaction.Commit(); return(supplier); } catch (Exception exception) { transaction.Rollback(); throw exception; } } }
public async Task <OrderDTO> OrderDelete(OrderDTO orderDTO) { var dataObject = orderDTO as ResturantOrder; _sqlServerContext.Remove <ResturantOrder>(dataObject); await _sqlServerContext.SaveChangesAsync(); return(orderDTO); }
public async Task <HttpResponseMessage> AddEnterprise(Enterprise enterprise) { HttpResponseMessage response; if (!ModelState.IsValid) { response = Request.CreateResponse(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(ModelState.ExpendErrors())); } else { enterprise.EnterpriseId = Guid.NewGuid(); _db.Enterprise.Add(enterprise); await _db.SaveChangesAsync(); var result = new { EnterpriseId = enterprise.EnterpriseId }; response = Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(result)); } return(response); }
public async Task <HttpResponseMessage> AddTenant(int id, TenantDetail tenantDetail) { if (await _db.User.AnyAsync(x => x.UserId == id)) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"User {id} does not exist."); } if (tenantDetail == null) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Parameter tenantDetail userDetailsList can not empty."); } var tenant = new Tenant { CreateByUserId = id, CreateTime = DateTime.Now, TenantDetail = tenantDetail }; _db.Tenant.Add(tenant); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(result))); }
public async Task <ActionResult <Utente> > RegisterUser(Utente utente) { if (!ModelState.IsValid) { return(BadRequest()); } utente.Monete = 0; _context.Utenti.Add(utente); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetUser), new { id = utente.Id }, utente)); }
public async Task <LocationDTO> LocationDelete(LocationDTO locationDTO) { var dataObject = locationDTO as Location; _sqlServerContext.Remove <Location>(dataObject); await _sqlServerContext.SaveChangesAsync(); return(locationDTO); }
public async Task AddAsync(T entity) { _dbContext.Set <T>().Add(entity); await _dbContext.SaveChangesAsync(); }
private async Task SaveChangesAsync() { await context.SaveChangesAsync(); }
public async Task Insert(T obj) { context.Set <T>().Add(obj); await context.SaveChangesAsync(); }
public async Task <HttpResponseMessage> AddUserByTenant(int id, UserDetail userDetail) { if (await _db.Tenant.AnyAsync(x => x.TenantId == id)) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"Tenant {id} does not exist."); } if (userDetail == null) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Parameter userDetail can not empty."); } if (await _db.UserDetail.AnyAsync(x => x.UserPrincipalName == userDetail.UserPrincipalName)) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Duplicate user principal name."); } _db.User.Add(new User { Token = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{userDetail.UserPrincipalName}:{userDetail.Password}")), CreateTime = DateTime.Now, UserDetail = userDetail, Tenant = new List <Tenant> { await _db.Tenant.SingleAsync(x => x.TenantId == id) } }); var result = await _db.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(result))); }