// DELETE: odata/UsersDeviceTokens(5) public async Task <IHttpActionResult> Delete([FromODataUri] int key) { UsersDeviceTokens usersDeviceTokens = await db.UsersDeviceTokens.FindAsync(key); if (usersDeviceTokens == null) { return(NotFound()); } db.UsersDeviceTokens.Remove(usersDeviceTokens); await db.SaveChangesAsync(); return(StatusCode(HttpStatusCode.NoContent)); }
// POST: odata/UsersDeviceTokens public async Task <IHttpActionResult> Post(UsersDeviceTokens usersDeviceTokens) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (usersDeviceTokens.token == null) { return(BadRequest("Token can't be null!")); } if (usersDeviceTokens.UserId == null) { return(BadRequest("User can't be null!")); } ApplicationUser user = db.Users.Find(usersDeviceTokens.UserId); if (user == null) { return(BadRequest("No matching user!")); } UsersDeviceTokens temp = db.UsersDeviceTokens.Where(a => a.UserId.Equals(usersDeviceTokens.UserId)).FirstOrDefault(); if (temp != null) { string[] listOfTokens = temp.token.Split(';'); if (!listOfTokens.Contains(usersDeviceTokens.token)) { temp.token = temp.token + ";" + usersDeviceTokens.token; temp.LastModificationDate = DateTime.Now; temp.ModifierId = core.getCurrentUser().Id; db.Entry(temp).State = EntityState.Modified; db.SaveChanges(); } return(Ok(temp)); } else { usersDeviceTokens.CreationDate = DateTime.Now; usersDeviceTokens.LastModificationDate = DateTime.Now; usersDeviceTokens.CreatorId = core.getCurrentUser().Id; usersDeviceTokens.ModifierId = core.getCurrentUser().Id; db.UsersDeviceTokens.Add(usersDeviceTokens); await db.SaveChangesAsync(); return(Created(usersDeviceTokens)); } }
// PUT: odata/UsersDeviceTokens(5) public async Task <IHttpActionResult> Put([FromODataUri] int key, Delta <UsersDeviceTokens> patch) { Validate(patch.GetEntity()); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UsersDeviceTokens usersDeviceTokens = await db.UsersDeviceTokens.FindAsync(key); if (usersDeviceTokens == null) { return(NotFound()); } patch.Put(usersDeviceTokens); try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UsersDeviceTokensExists(key)) { return(NotFound()); } else { throw; } } return(Updated(usersDeviceTokens)); }