Exemple #1
0
        public IHttpActionResult Put(int id, [FromBody] PaymentCardVM item)
        {
            if (item is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.PaymentCard)));
            }
            if (id != item.Id)
            {
                return(BadRequest(ErrorMsg.InvalidIdMatchingWith(TypeOfItems.PaymentCard)));
            }
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            var isModelValid = item.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            string      userId           = User.Identity.GetUserId();
            PaymentCard cardToBeModified = db.PaymentCards.FirstOrDefault(w => w.Id == id && w.UserId == userId);

            if (cardToBeModified is null)
            {
                return(BadRequest(ErrorMsg.ItemNotFound(TypeOfItems.PaymentCard)));
            }
            cardToBeModified.ModifyTo(item);
            db.SaveChanges();
            return(Ok());
        }
        //PUT api/Passwords/5
        public IHttpActionResult Put(int id, [FromBody] PasswordVM password)
        {
            //check if the password id and the id match
            if (password is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Password)));
            }
            if (id != password.Id)
            {
                return(BadRequest(ErrorMsg.InvalidIdMatchingWith(TypeOfItems.Password)));
            }
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            //check is model is valid
            var isModelValid = password.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            //get current user id and modify it
            string   userId = User.Identity.GetUserId();
            Password passwordToBeModified = db.Passwords.FirstOrDefault(w => w.Id == id && w.UserId == userId);

            if (passwordToBeModified is null)
            {
                return(BadRequest(ErrorMsg.ItemNotFound(TypeOfItems.Password)));
            }
            //modify it
            passwordToBeModified.ModifyTo(password);
            db.SaveChanges();
            return(Ok());
        }
        //PUT api/Wifi/5
        public IHttpActionResult Put(int id, [FromBody] WifiVM wifi)
        {
            //check if the wifi and the id match
            if (wifi is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Wifi)));
            }
            if (id != wifi.Id)
            {
                return(BadRequest(ErrorMsg.InvalidIdMatchingWith(TypeOfItems.Wifi)));
            }
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            //check if wifi is valid
            var isModelValid = wifi.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            //get current user id and modify wifi
            string userId           = User.Identity.GetUserId();
            Wifi   wifiToBeModified = db.Wifis.FirstOrDefault(w => w.Id == id && w.UserId == userId);

            if (wifiToBeModified is null)
            {
                return(BadRequest(ErrorMsg.ItemNotFound(TypeOfItems.Wifi)));
            }
            wifiToBeModified.ModifyTo(wifi);
            db.SaveChanges();
            return(Ok());
        }
Exemple #4
0
        //PUT api/Notes/5
        public IHttpActionResult Put(int id, [FromBody] NoteVM note)
        {
            if (note is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Note)));
            }
            if (id != note.Id)
            {
                return(BadRequest(ErrorMsg.InvalidIdMatchingWith(TypeOfItems.Note)));
            }
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            var isModelValid = note.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            string userId           = User.Identity.GetUserId();
            Note   noteToBeModified = db.Notes.FirstOrDefault(w => w.Id == id && w.UserId == userId);

            if (noteToBeModified is null)
            {
                return(BadRequest(ErrorMsg.ItemNotFound(TypeOfItems.Note)));
            }
            noteToBeModified.ModifyTo(note);
            db.SaveChanges();
            return(Ok());
        }
        //GET api/Passwords/5
        public IHttpActionResult Get(int id)
        {
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            string   userId   = User.Identity.GetUserId();
            Password password = db.Passwords.FirstOrDefault(w => w.Id == id && w.UserId == userId);//get the current password from user

            if (password is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Password))); //check if that exists
            }
            password.NumOfVisits++;                                                  //modify when is last visited
            db.SaveChanges();                                                        //save the changes
            return(Ok(new PasswordVM(password)));                                    //send
        }
Exemple #6
0
        public IHttpActionResult Get(int id)
        {
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            string      userId      = User.Identity.GetUserId();
            PaymentCard paymentCard = db.PaymentCards.FirstOrDefault(n => n.Id == id && n.UserId == userId);

            if (paymentCard is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.PaymentCard)));
            }
            paymentCard.NumOfVisits++;
            db.SaveChanges();
            return(Ok(new PaymentCardVM(paymentCard)));
        }
Exemple #7
0
        //GET api/Notes/5
        public IHttpActionResult Get(int id)
        {
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            string userId = User.Identity.GetUserId();
            Note   note   = db.Notes.FirstOrDefault(n => n.Id == id && n.UserId == userId);

            if (note is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Note)));
            }
            note.NumOfVisits++;
            db.SaveChanges();
            return(Ok(new NoteVM(note)));
        }
        //POST api/Passwords
        public IHttpActionResult Post([FromBody] PasswordVM password)
        {
            if (password is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Password)));
            }
            var isModelValid = password.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            string userId = User.Identity.GetUserId();

            db.Passwords.Add(new Password(password, userId));
            db.SaveChanges();
            return(Ok());
        }
        //POST api/Wifi
        public IHttpActionResult Post([FromBody] WifiVM wifi)
        {
            if (wifi is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Wifi)));
            }
            var isModelValid = wifi.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            string userId = User.Identity.GetUserId();

            db.Wifis.Add(new Wifi(wifi, userId));
            db.SaveChanges();
            return(Ok());
        }
        //GET api/Wifis/5
        public IHttpActionResult Get(int id)
        {
            if (id <= 0)
            {
                return(BadRequest(ErrorMsg.InvalidId));
            }
            string userId = User.Identity.GetUserId();
            //get current wifi based on userId
            Wifi wifi = db.Wifis.FirstOrDefault(w => w.Id == id && w.UserId == userId);

            if (wifi is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Wifi)));
            }
            wifi.NumOfVisits++;
            db.SaveChanges();
            return(Ok(new WifiVM(wifi)));
        }
Exemple #11
0
        //POST api/Notes
        public IHttpActionResult Post([FromBody] NoteVM note)
        {
            if (note is null)
            {
                return(BadRequest(ErrorMsg.ItemDoesNotExist(TypeOfItems.Note)));
            }
            var isModelValid = note.IsModelValid();

            if (!string.IsNullOrEmpty(isModelValid))
            {
                return(BadRequest(isModelValid));
            }
            string userId = User.Identity.GetUserId();

            db.Notes.Add(new Note(note, userId));
            db.SaveChanges();
            return(Ok());
        }