Exemple #1
0
        public IHttpActionResult Edit(ContactEditModel theContact)
        {
            Reply  reply  = new Reply();
            String json   = "";
            var    userID = User.Identity.GetUserId(); //get user ID  

            UserEmergency userEmergency = new UserEmergency();

            userEmergency.UserProfileId         = userID;
            userEmergency.EmergencyContactPhone = theContact.pre.EmergencyContactPhone;
            userEmergency.ECname = theContact.pre.ECname;

            UserEmergency newContact = new UserEmergency();

            newContact.UserProfileId         = userID;
            newContact.EmergencyContactPhone = theContact.now.EmergencyContactPhone;
            newContact.ECname = theContact.now.ECname;

            if (ModelState.IsValid)
            {
                //Check does the phone exist if not store Contactphone to table EmergencyContacts
                EmergencyContact thisContact = db.EmergencyContact.Find(newContact.EmergencyContactPhone);
                if (thisContact == null)
                {
                    createNewContact(newContact.EmergencyContactPhone);
                }


                //Check whether old userEmergency exist
                var result = db.UserEmergency.Find(userEmergency.EmergencyContactPhone, userEmergency.UserProfileId);
                if (result == null)
                {
                    reply.result = "failed";
                    reply.errors = "Contact do not exist";
                    return(BadRequest(JsonConvert.SerializeObject(reply)));
                }
                var checkNew = db.UserEmergency.Find(newContact.EmergencyContactPhone, newContact.UserProfileId);
                if (checkNew != null && newContact.EmergencyContactPhone != userEmergency.EmergencyContactPhone)
                {
                    reply.result = "failed";
                    reply.errors = "The contact phone number already exist";
                    return(BadRequest(JsonConvert.SerializeObject(reply)));
                }
                db.UserEmergency.Remove(result);
                db.UserEmergency.Add(newContact);
                db.SaveChanges();
                reply.result = "Edit success";
                json         = JsonConvert.SerializeObject(reply);


                reply.result = "contact create, edit success";
                json         = JsonConvert.SerializeObject(reply);
                return(Ok(json));
            }
            reply.result = "failed";
            reply.errors = "Data Type not validate";
            json         = JsonConvert.SerializeObject(reply);
            return(Ok(json));
        }
Exemple #2
0
        // POST: UserEmergencies/getUser
        //Get a single user profile.
        public Users getUser(UserEmergency theProfile)
        {
            Users result = new Users();

            result.phone = theProfile.EmergencyContactPhone;
            UserProfile      thisUser = db.UserProfile.Find(theProfile.UserProfileId);
            userProfileModel theUser  = new userProfileModel();

            theUser.id         = thisUser.Id;
            theUser.address    = thisUser.Address;
            theUser.FirstName  = thisUser.FirstName;
            theUser.LastName   = thisUser.LastName;
            theUser.Gender     = thisUser.Gender;
            result.userDetails = theUser;
            return(result);
        }
Exemple #3
0
        public IHttpActionResult DeleteConfirmed(EmergencyDelete theEmergency)
        {
            Reply  reply  = new Reply();
            String json   = "";
            var    userID = User.Identity.GetUserId(); //get user ID  

            UserEmergency userEmergency = db.UserEmergency.Find(theEmergency.EmergencyContactPhone, userID);

            if (userEmergency == null)
            {
                reply.result = "failed";
                reply.errors = "Not Found";
                json         = JsonConvert.SerializeObject(reply);
                return(BadRequest(json));
            }
            db.UserEmergency.Remove(userEmergency);
            db.SaveChanges();
            reply.result = "success";
            json         = JsonConvert.SerializeObject(reply);
            return(Ok(json));
        }
Exemple #4
0
        public IHttpActionResult Create(ContactModel theContact)
        {
            Reply  reply  = new Reply();
            String json   = "";
            var    userID = User.Identity.GetUserId(); //get user ID 

            if (ModelState.IsValid)
            {
                //check does the phone number already in emergencyContact table. create one if not exist.
                if (db.EmergencyContact.Find(theContact.EmergencyContactPhone) == null)
                {
                    createNewContact(theContact.EmergencyContactPhone);
                }

                UserEmergency userEmergency = new UserEmergency();
                userEmergency.UserProfileId         = userID;
                userEmergency.EmergencyContactPhone = theContact.EmergencyContactPhone;
                userEmergency.ECname = theContact.ECname;

                if (db.UserEmergency.Find(userEmergency.EmergencyContactPhone, userEmergency.UserProfileId) != null)
                {
                    reply.result = "failed";
                    reply.errors = "Existed";
                    json         = JsonConvert.SerializeObject(reply);
                    return(BadRequest(json));
                }
                db.UserEmergency.Add(userEmergency);
                db.SaveChanges();
                reply.result = "success";
                json         = JsonConvert.SerializeObject(reply);
                return(Ok(json));
            }
            reply.result = "failed";
            reply.errors = "data not match";
            json         = JsonConvert.SerializeObject(reply);
            return(BadRequest(json));
        }