Ejemplo n.º 1
0
 public ActionResult <List <Contacts> > GetContacts([FromQuery] int userId)
 {
     using (var db = new PhonebookDBContext())
     {
         return(db.Contacts.Where(c => c.UserId == userId).ToList());
     }
 }
Ejemplo n.º 2
0
        public ActionResult Post([FromBody] Contacts contact)
        {
            using (var db = new PhonebookDBContext())
            {
                db.Contacts.Add(contact);
                db.SaveChanges();

                return(Created("api/contacts", contact));
            }
        }
Ejemplo n.º 3
0
        public ActionResult <Contacts> GetContactById(int id)
        {
            using (var db = new PhonebookDBContext())
            {
                var con = db.Contacts.FirstOrDefault(c => c.Id == id);

                if (con != null)
                {
                    return(con);
                }
                return(NotFound("Not found contact with a same Id"));
            }
        }
Ejemplo n.º 4
0
        public ActionResult Delete(int id)
        {
            using (var db = new PhonebookDBContext())
            {
                var con = db.Contacts.FirstOrDefault(c => c.Id == id);

                if (con != null)
                {
                    db.Contacts.Remove(con);
                    db.SaveChanges();

                    return(Ok());
                }

                return(NotFound("Not found contact with a same Id"));
            }
        }
Ejemplo n.º 5
0
        public ActionResult Put(int id, [FromBody] Contacts contact)
        {
            using (var db = new PhonebookDBContext())
            {
                var con = db.Contacts.FirstOrDefault(c => c.Id == id);

                if (con != null)
                {
                    con.Name   = contact.Name;
                    con.Phone  = contact.Phone;
                    con.UserId = contact.UserId;

                    db.SaveChanges();

                    return(new ObjectResult(contact));
                }
                return(NotFound("Not found contact with a same Id"));
            }
        }
Ejemplo n.º 6
0
        public IActionResult Post([FromBody] Users _user)
        {
            using (var db = new PhonebookDBContext())
            {
                var user = db.Users.FirstOrDefault(u => u.Lgn == _user.Lgn);

                if (user != null)
                {
                    if (user.Pwd == MD5Converter.GetHashFromString(_user.Pwd))
                    {
                        return(Ok(user.Id));
                    }
                    else
                    {
                        return(Unauthorized("Invalid password"));
                    }
                }

                return(NotFound("User with login " + _user.Lgn + " does not exist"));
            }
        }