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 IActionResult Post([FromBody] ControlModel control) { control.ControlModelId = 0; _context.ControlModels.Add(control); _context.SaveChanges(); return(Ok()); }
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 bool Create(Order order) { if(order == null) return false; if(!ModelState.IsValid) return false; db.Orders.Add(order); db.SaveChanges(); return true; }
// 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 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)); }
// PUT api/Media/5 public IHttpActionResult PutVideo(int id, Media media) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Media newImage = new Media(); int times = db.Media.Where(d => d.Update_ID == id).Count(); // this is working if (times < 5) { newImage.MediaName = media.MediaName; newImage.VideoUrl = media.VideoUrl; newImage.Update_ID = id; newImage.Discriminator = "video"; db.Media.Add(newImage); try { db.SaveChanges(); } catch (DbUpdateException) { throw; } } return(CreatedAtRoute("DefaultApi", new { id = media.MediaID }, media)); }
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 JsonResult AddToBasket(int?id) { if (!id.HasValue) { return(Json(new { success = false, responseText = "The id of product is Invalid" }, JsonRequestBehavior.AllowGet)); } var product = _context.Products.SingleOrDefault(p => p.ID == id.Value); if (product.Amount == 0) { return(Json(new { success = false, responseText = "This product is not available!" }, JsonRequestBehavior.AllowGet)); } if (product == null) { return(Json(new { success = false, responseText = "This product is not available!" }, JsonRequestBehavior.AllowGet)); } if (Session["userBasket"] == null) { Session["userBasket"] = new List <Product>(); } var list = Session["userBasket"] as List <Product>; list.Add(product); Session["userBasket"] = list; --product.Amount; _context.SaveChanges(); return(Json(new { success = true, responseText = "Product added to basket", basketItemsCount = list.Count }, JsonRequestBehavior.AllowGet)); }
public void Insert(ArpenspModel arpenspModel) { using (context) { context.Arpensp.Add(arpenspModel); context.SaveChanges(); } }
public TodoItem Add(TodoItem todoitem) { var addedTodoItem = _context.Add(todoitem); _context.SaveChanges(); todoitem.Id = addedTodoItem.Entity.Id; return(todoitem); }
public IActionResult Post([FromBody] Equips equip) { if (ModelState.IsValid) { db.Add(equip); db.SaveChanges(); return(CreatedAtAction("GetEquips", new { id = equip.EquipID }, equip)); } return(BadRequest(ModelState)); }
public ActionResult Create([Bind(Include = "Id,UserId,TimeStamp,TodaySteps,Distance,Calories")] StepInfo stepInfo) { if (ModelState.IsValid) { db.StepInfos.Add(stepInfo); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(stepInfo)); }
public ActionResult Create([Bind(Include = "Id,UserName,Password,Phone,Height,Weight,DeviceToken")] UserInfo userInfo) { if (ModelState.IsValid) { db.UserInfos.Add(userInfo); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(userInfo)); }
public IHttpActionResult PutUserInfo(string id, UserInfo userInfo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != userInfo.Id) { return(BadRequest()); } if (HttpContext.Current.Session?["UserId"] == null) { return(Unauthorized()); } if (Equals(HttpContext.Current.Session?["UserId"], id) == false) { return(Unauthorized()); } var entity = db.UserInfos.Find(id); if (entity == null) { return(NotFound()); } entity.UserName = userInfo.UserName ?? entity.UserName; entity.Password = userInfo.Password ?? entity.Password; entity.Phone = userInfo.Phone ?? entity.Phone; entity.DeviceToken = userInfo.DeviceToken ?? entity.DeviceToken; entity.Weight = userInfo.Weight > 0 ? userInfo.Weight : entity.Weight; entity.Height = userInfo.Height > 0 ? userInfo.Height : entity.Height; db.Entry(entity).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserInfoExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IActionResult Create([FromBody] WebApiClass item) { if (item == null) { return(BadRequest()); } _context.WebApiItem.Add(item); _context.SaveChanges(); return(CreatedAtRoute("GetTodo", new { id = item.Id }, item)); }
private void Seed() { var rand = new Random(); using var context = new WebApiContext(DbContextOptions); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.UserGroups.AddRange(new UserGroup { Code = UserGroupCode.Admin, Description = rand.Next().ToString() }, new UserGroup { Code = UserGroupCode.User, Description = rand.Next().ToString() }); context.UserStates.AddRange(new UserState { Code = UserStateCode.Active, Description = rand.Next().ToString() }, new UserState { Code = UserStateCode.Blocked, Description = rand.Next().ToString() }); context.SaveChanges(); var groups = context.UserGroups.Select(g => g.Id).ToList(); var states = context.UserStates.Select(s => s.Id).ToList(); var usersToAdd = new List <User>(); for (var i = 0; i < 10; i++) { usersToAdd.Add(new User { Login = rand.Next().ToString(), PasswordHash = rand.Next().ToString(), Salt = rand.Next().ToString(), CreatedDate = DateTime.Now - TimeSpan.FromSeconds(rand.Next()), GroupId = groups[rand.Next(groups.Count)], StateId = states[rand.Next(states.Count)] }); } context.AddRange(usersToAdd); context.SaveChanges(); }
public bool Create(Customer customer) { if (customer == null) { return(false); } if (!ModelState.IsValid) { return(false); } db.Customers.Add(customer); db.SaveChanges(); return(true); }
public async Task <ActionResult <User> > Get(int id) { var user = await _context.Users.FindAsync(id); if (user == null) { var data = System.IO.File.ReadAllText("./data.json"); List <User> userData = JsonConvert.DeserializeObject <List <User> >(data); // return userData; userData.ForEach(p => { User User = new User() { FirstName = p.FirstName, LastName = p.LastName, Email = p.Email, Gender = p.Gender, Phone = p.Phone, }; _context.Users.Add(User); _context.SaveChanges(); }); // Console.WriteLine(userData); } return(user); }
public void Can_delete_item() { using (var context = new WebApiContext(ContextOptions)) { User usr = new User() { Name = "Test User" }; context.Users.Add(usr); Ticket t = new Ticket() { Name = "test write", User = usr }; context.Tickets.Add(t); context.SaveChanges(); int delete = t.Id; var controller = new TicketsController(context); var item = controller.Delete(delete); Ticket newticket = context.Tickets.FirstOrDefault(x => x.Id == delete); Assert.Null(newticket); Assert.IsType <ActionResult <Ticket> >(item.Result); } }
public void Can_post_item() { using (var context = new WebApiContext(ContextOptions)) { User usr = new User() { Name = "Test User" }; context.Users.Add(usr); context.SaveChanges(); Ticket t = new Ticket() { Name = "test write", UserId = usr.Id }; var controller = new TicketsController(context); //t.Name = "new value"; var item = controller.Post(t).Result; Ticket newticket = context.Tickets.Find(t.Id); Assert.IsType <ActionResult <Ticket> >(item); Assert.Equal(t, newticket); } }
public void Can_put_item() { using (var context = new WebApiContext(ContextOptions)) { User usr = new User() { Name = "Test User" }; context.Users.Add(usr); Ticket t = new Ticket() { Name = "test write", User = usr }; context.Tickets.Add(t); context.SaveChanges(); Ticket newticket = context.Tickets.Find(t.Id); var controller = new TicketsController(context); t.Name = "new value"; var item = controller.Put(t); Assert.IsType <OkObjectResult>(item.Result); Assert.Equal(newticket, t); Assert.Equal("new value", newticket.Name); } }
public IHttpActionResult Get() { if (HttpContext.Current.Session?["UserId"] == null) { return(Unauthorized()); } string userId = HttpContext.Current.Session["UserId"] as string; var random = new Random(); //生成近30天的数据,每天10条 for (int i = 0; i < 30; i++) { for (int j = 0; j < 10; j++) { var steps = random.Next(100, 10000); var stepInfo = new StepInfo { TimeStamp = (long)(DateTime.Now - TimeSpan.FromDays(i) - UnixStartTime).TotalMilliseconds, TodaySteps = steps, Calories = steps * 0.1f, Distance = steps * 0.3f, UserId = userId }; db.StepInfoes.Add(stepInfo); } } db.SaveChanges(); return(StatusCode(HttpStatusCode.NoContent)); }
private List <Worker> GetWorkers(List <WorkerData> workersData, WebApiContext context) { List <Worker> workers = new List <Worker>(); foreach (WorkerData workerData in workersData) { Worker worker = context.Workers.FirstOrDefault(w => w.Name == workerData.Name); if (worker == null) { worker = new Worker() { Name = workerData.Name, Host = workerData.Host, Port = workerData.Port, Endpoint = workerData.Endpoint }; context.Workers.Add(worker); } else { worker.Host = workerData.Host; worker.Port = workerData.Port; worker.Endpoint = workerData.Endpoint; context.Workers.Update(worker); } workers.Add(worker); } context.SaveChanges(); return(workers); }
public IHttpActionResult PostEndUser(AssignedProductData[] selectedProducts) { var e = User.Identity.Name; if (e == null) { return(null); } var endUserToUpdate = db.EndUser .Include(i => i.ProductNews) .Where(i => i.Email == e) .Single(); try { UpdateEndUserProducts(selectedProducts, endUserToUpdate); db.SaveChanges(); } catch (Exception) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } PopulateAssignedProductData(endUserToUpdate); return(Ok(endUserToUpdate)); }
public void Insert(CadespModel cadespModel) { using (context) { context.Cadesp.Add(cadespModel); context.SaveChanges(); } }
private string setToken(userLogin user) { user.Token = Guid.NewGuid().ToString(); _context.Update(user); _context.SaveChanges(); return(user.Token); }
public void Insert(CensecModel censecModel) { using (context) { context.Censec.Add(censecModel); context.SaveChanges(); } }
public void Insert(JucespModel jucespModel) { using (context) { context.Jucesp.Add(jucespModel); context.SaveChanges(); } }
public void Insert(DetranModel detranModel) { using (context) { context.Detran.Add(detranModel); context.SaveChanges(); } }