public async Task <IActionResult> PutShop(int id, [FromBody] ShopDto shop) { if (id != shop.Id) { return(BadRequest()); } if (shop.OwnerId != AuthenticatedUserId()) { return(Unauthorized("Shop does not belong to you")); } _context.Entry(_mapper.Map <Shop>(shop)).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShopExists(id)) { return(NotFound()); } throw; } return(NoContent()); }
public bool Change(Order order) { if(order == null) return false; if(!ModelState.IsValid) return false; db.Orders.Attach(order); db.Entry(order).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return true; }
public async Task <IActionResult> PutUser(int id, UserPutDto user) { if (id != user.Id) { return(BadRequest()); } await using var transaction = await _context.Database.BeginTransactionAsync(IsolationLevel.Serializable); try { var entity = await _context.GetActiveUsers() .FirstOrDefaultAsync(u => u.Id == user.Id); if (entity == null) { return(NotFound()); } if (await _context.UserGroups.FindAsync(user.GroupId) == null) { return(BadRequest()); } if (!await _adminElevation.CanEnterGroup(user.GroupId, user.Id)) { return(BadRequest()); } entity.Login = user.Login; entity.GroupId = user.GroupId; _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(); await transaction.CommitAsync(); return(Ok()); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } return(Conflict()); } }
public bool Change(Customer customer) { if (customer == null) { return(false); } if (!ModelState.IsValid) { return(false); } db.Customers.Attach(customer); db.Entry(customer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(true); }
public IHttpActionResult PutEmployee(int id, Employee employee) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != employee.EmployeeId) { return(BadRequest()); } db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <ActionResult <Users> > Put(int id, Users item) { _logger.LogInformation(LoggingEvents.UpdateItem, "PUT USER"); try { if (id != item.User_ID) { return(BadRequest()); } using (var db = new WebApiContext()) { db.Entry(item).State = EntityState.Modified; await db.SaveChangesAsync(); _logger.LogInformation(LoggingEvents.UpdateItem, "PUT Successful"); return(NoContent()); } } catch (Exception ex) { _logger.LogError(LoggingEvents.Error, ex, "AN ERROR OCCURRED."); return(null); } }// THE END PUT
public async Task <IHttpActionResult> Putcursistcursus(int id, cursistcursus cursistcursus) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != cursistcursus.CURSUSCODE) { return(BadRequest()); } db.Entry(cursistcursus).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!cursistcursusExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutEndereco(int id, Endereco endereco) { if (id != endereco.EnderecoId) { return(BadRequest()); } _context.Entry(endereco).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EnderecoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IHttpActionResult PutTaskDetail(int id, TaskDetail taskDetail) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != taskDetail.TaskId) { return(BadRequest()); } db.Entry(taskDetail).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!TaskDetailExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutUser(int id, UserDto userDto) { if (id != userDto.Id) { return(BadRequest()); } if (id != AuthenticatedUserId()) { return(Unauthorized("You are not this user")); } var user = _mapper.Map <User>(userDto); _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } throw; } return(NoContent()); }
public IHttpActionResult PutBillboardModel(int id, BillboardModel billboardModel) { if (id != billboardModel.id) { return(BadRequest()); } db.Entry(billboardModel).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!BillboardModelExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task ChangeAdminUserToAdminGroup() { await using var context = new WebApiContext(_seeder.DbContextOptions); var controller = CreateController(context); var users = context.Users.ToList(); foreach (var user in users) { user.GroupId = _userGroup.Id; context.Entry(user).State = EntityState.Modified; } var admin = users[new Random().Next(users.Count)]; admin.GroupId = _adminGroup.Id; await context.SaveChangesAsync(); var changes = new UserPutDto { Id = admin.Id, Login = "******", GroupId = _adminGroup.Id, }; var result = await controller.PutUser(admin.Id, changes); result.ShouldBeAssignableTo <OkResult>(); var changedUser = await context.Users.FindAsync(admin.Id); changedUser.Login.ShouldBe(changes.Login); changedUser.GroupId.ShouldBe(changes.GroupId); }
public async Task ChangeUserToAdminWhenAdminPresentFails() { await using var context = new WebApiContext(_seeder.DbContextOptions); var controller = CreateController(context); var users = context.Users.ToList(); foreach (var user in users) { user.GroupId = _userGroup.Id; context.Entry(user).State = EntityState.Modified; } users[new Random().Next(users.Count)].GroupId = _adminGroup.Id; await context.SaveChangesAsync(); var userToChange = await context.Users.FirstAsync(u => u.Group.Code != UserGroupCode.Admin); var oldData = new { userToChange.Login, userToChange.GroupId }; var changes = new UserPutDto { Id = userToChange.Id, Login = "******", GroupId = _adminGroup.Id, }; var result = await controller.PutUser(userToChange.Id, changes); result.ShouldBeAssignableTo <BadRequestResult>(); userToChange.Login.ShouldBe(oldData.Login); userToChange.GroupId.ShouldBe(oldData.GroupId); }
public async Task ChangeUserToAdminWhenNoAdminPresent() { await using var context = new WebApiContext(_seeder.DbContextOptions); var controller = CreateController(context); await foreach (var user in context.Users) { user.GroupId = _userGroup.Id; context.Entry(user).State = EntityState.Modified; } await context.SaveChangesAsync(); var userToChange = await context.Users.FirstAsync(); var changes = new UserPutDto { Id = userToChange.Id, Login = "******", GroupId = _adminGroup.Id, }; var result = await controller.PutUser(userToChange.Id, changes); result.ShouldBeAssignableTo <OkResult>(); var changedUser = await context.Users.FindAsync(userToChange.Id); changedUser.Login.ShouldBe(changes.Login); changedUser.GroupId.ShouldBe(changes.GroupId); }
public async Task AddAdminWhenAdminPresentFails() { await using var context = new WebApiContext(_seeder.DbContextOptions); var controller = CreateController(context); var users = context.Users.ToList(); foreach (var user in users) { user.GroupId = _userGroup.Id; context.Entry(user).State = EntityState.Modified; } users[new Random().Next(users.Count)].GroupId = _adminGroup.Id; await context.SaveChangesAsync(); var userToAdd = new UserPostDto { Login = "******", Password = "******", GroupId = _adminGroup.Id }; var result = await controller.PostUser(userToAdd); result.Result.ShouldBeAssignableTo <BadRequestResult>(); context.Users.Count().ShouldBe(10); }
public async Task AddAdminWhenNoAdminPresent() { await using var context = new WebApiContext(_seeder.DbContextOptions); var controller = CreateController(context); await foreach (var user in context.Users) { user.GroupId = _userGroup.Id; context.Entry(user).State = EntityState.Modified; } await context.SaveChangesAsync(); var userToAdd = new UserPostDto { Login = "******", Password = "******", GroupId = _adminGroup.Id }; var result = await controller.PostUser(userToAdd); result.Result.ShouldBeAssignableTo <CreatedAtActionResult>(); var addedUser = (UserGetDto)((CreatedAtActionResult)result.Result).Value; (await context.Users.FindAsync(addedUser.Id)).ShouldNotBeNull(); }
public async Task <IHttpActionResult> Putschipklasse(int id, schipklasses schipklasse) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != schipklasse.KLASSEID) { return(BadRequest()); } db.Entry(schipklasse).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!schipklasseExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutShopList(int id, ShopList shopList) { if (id != shopList.ID) { return(BadRequest()); } _context.Entry(shopList).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShopListExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
// PUT api/Person/5 public HttpResponseMessage PutPerson(int id, Person person) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (id != person.ID) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } db.Entry(person).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task <IHttpActionResult> PutCompraGado(int id, CompraGado compraGado) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != compraGado.Id) { return(BadRequest()); } db.Entry(compraGado).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CompraGadoExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutCliente(int id, Cliente cliente) { if (id != cliente.ClienteId) { return(BadRequest()); } _context.Entry(cliente).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ClienteExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutObjetoCalculo(int id, ObjetoCalculo objetoCalculo) { if (id != objetoCalculo.Id) { return(BadRequest()); } _context.Entry(objetoCalculo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ObjetoCalculoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutImovel(int id, Imovel imovel) { if (id != imovel.Id) { return(BadRequest()); } _context.Entry(imovel).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ImovelExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutRegistration(int id, Registration registration) { if (id != registration.Id) { return(BadRequest()); } _context.Entry(registration).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RegistrationExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProject([FromRoute] int id, [FromBody] Project project) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != project.Id) { return(BadRequest()); } _context.Entry(project).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProjectExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutEditorial(Guid id, Editorial editorial) { if (id != editorial.IdEditoria) { return(BadRequest()); } _context.Entry(editorial).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EditorialExists(id)) { return(NotFound()); } else { throw; } } return(Ok(new { status = true })); }
public async Task <IActionResult> PutBicicleta(int id, Bicicleta bicicleta) { if (id != bicicleta.Id) { return(BadRequest()); } _context.Entry(bicicleta).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BicicletaExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IHttpActionResult PutArea(int id, Area area) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != area.Id) { return(BadRequest()); } db.Entry(area).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!AreaExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutProprietario(int id, Proprietario proprietario) { if (id != proprietario.Id) { return(BadRequest()); } _context.Entry(proprietario).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProprietarioExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutLibro(Guid id, Libro libro) { if (id != libro.IdLibro) { return(BadRequest()); } _context.Entry(libro).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!LibroExists(id)) { return(NotFound()); } else { throw; } } return(Ok(new { status = true })); }