public IHttpActionResult UpdateEntry(int id, [FromBody] Entry entry) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != entry.Id) { return(BadRequest()); } try { using (var context = new AppDbConext()) { var oldEntry = context.entries.FirstOrDefault(n => n.Id == id); if (oldEntry == null) { return(NotFound()); } oldEntry.Description = entry.Description; oldEntry.IsExpense = entry.IsExpense; oldEntry.Value = entry.Value; context.SaveChanges(); return(Ok("Entry Updated Successfully")); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult PostEntry([FromBody] Entry entry) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { using (var context = new AppDbConext()) { context.entries.Add(entry); context.SaveChanges(); return(Ok("Entry Was Created")); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult Register([FromBody] User user) { try { using (var context = new AppDbConext()) { var exists = context.users.Any(n => n.UserName == user.UserName); if (exists) { return(BadRequest("User Already Exists!!!!")); } context.users.Add(user); context.SaveChanges(); return(Ok(createToken(user))); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult DeleteEntry(int id) { try { using (var context = new AppDbConext()) { var entry = context.entries.First(n => n.Id == id); if (entry == null) { return(NotFound()); } else { context.entries.Remove(entry); } context.SaveChanges(); return(Ok("Entry Deleted")); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }