コード例 #1
0
        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));
            }
        }
コード例 #2
0
 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));
     }
 }
コード例 #3
0
        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));
            }
        }
コード例 #4
0
 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));
     }
 }