Example #1
0
        public void CreateDefaultGroup(string groupKey, long userId)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    var groupName = "group" + userId;
                    using (var ctx = new ProjectEntities())

                    {
                        Group_table grpObj = new Group_table();
                        grpObj.group_name = groupName;
                        grpObj.user_id    = userId;
                        grpObj.status     = "Active";

                        ctx.Group_table.Add(grpObj);
                        ctx.SaveChanges();
                        scope.Complete();
                        creategroupkey(groupName, userId, groupKey);
                    }
                }
            }
            catch (Exception e)
            {
                var response = Request.CreateResponse <Exception>(System.Net.HttpStatusCode.BadRequest, e);
            }
        }
Example #2
0
        public HttpResponseMessage PostAddAdmin(string adminUserName, string adminPassword, string adminFirsName, string adminLastName, string adminTelephone, string adminAddress, string adminAliasMailId, string adminEmailId, string adminSkypeId, string publicKey, string groupKey)
        {
            var key             = "b14ca5898a4e4133bbce2ea2315a1916";
            var str             = adminPassword;
            var encryptedString = AesOperationController.EncryptString(key, str);

            DataSet dsData = new DataSet("admin");

            cnn = new SqlConnection(cfmgr);
            cnn.Open();
            string userType = "admin"; // userType = instructor or student or admin
            int    userId   = 0;

            //  MessageBox.Show(adminUserName);
            //Console.WriteLine(adminUserName);
            //first add to register table then to the student table.
            using (var ctx = new ProjectEntities())
            {
                register objreg = new register();
                objreg.userName  = adminUserName;
                objreg.password  = encryptedString;
                objreg.userType  = userType;
                objreg.publickey = publicKey;
                ctx.registers.Add(objreg);
                ctx.SaveChanges();


                var user = ctx.registers.Where(x => x.userName == adminUserName).Select(reg => new
                {
                    userId = reg.userId
                }).ToList();
                userId = Convert.ToInt32(user[0].userId);

                var         groupName = "defaultgroup" + userId;
                Group_table grpObj    = new Group_table();
                grpObj.group_name = groupName;
                grpObj.user_id    = userId;
                grpObj.status     = "Active";

                ctx.Group_table.Add(grpObj);
                ctx.SaveChanges();
                creategroupkey(groupName, userId, groupKey);
            }

            Admin adm = new Admin();

            adm.idAdmin     = adminUserName;
            adm.firstName   = adminFirsName;
            adm.lastName    = adminLastName;
            adm.userId      = userId;
            adm.telephone   = adminTelephone;
            adm.address     = adminAddress;
            adm.aliasMailId = adminAliasMailId;
            adm.emailId     = adminEmailId;
            adm.skypeId     = adminSkypeId;
            // now add to instructor table.


            try
            {
                SqlCommand comm = new SqlCommand("Insert into admin(idAdmin,firstName,lastName,userId,telephone,address,aliasMailId,emailId,skypeId) values('"
                                                 + adm.idAdmin
                                                 + "','" + adm.firstName
                                                 + "','" + adm.lastName
                                                 + "','" + adm.userId
                                                 + "','" + adm.telephone
                                                 + "','" + adm.address
                                                 + "','" + adm.aliasMailId
                                                 + "','" + adm.emailId
                                                 + "','" + adm.skypeId
                                                 + "')", cnn);
                SqlDataAdapter sqlada = new SqlDataAdapter(comm);
                sqlada.Fill(dsData);
                cnn.Close();
                var response = Request.CreateResponse <Admin>(System.Net.HttpStatusCode.Accepted, adm);
                return(response);
            }
            catch (Exception e)
            {
                var response = Request.CreateResponse <Admin>(System.Net.HttpStatusCode.BadRequest, adm);
                cnn.Close();
                return(response);
            }
        }
Example #3
0
        public HttpResponseMessage PostAddStudent(string userName, string password, string firsName, string lastName, string telephone, string address, string aliasMailId, string emailId, string skypeId, string publickey, string groupKey)
        {
            var     key             = "b14ca5898a4e4133bbce2ea2315a1916";
            var     str             = password;
            var     encryptedString = AesOperationController.EncryptString(key, str);
            DataSet dsData          = new DataSet("register");

            cnn = new SqlConnection(cfmgr);
            cnn.Open();
            string userType = "student"; // userType = instructor or student or admin
            int    userId   = 0;

            //first add to register table then to the student table.
            try
            {
                SqlCommand comm = new SqlCommand("Insert into register(userName,password,userType,publicKey) values('"
                                                 + userName
                                                 + "','" + encryptedString
                                                 + "','" + userType
                                                 + "','" + publickey
                                                 + "')", cnn);
                SqlDataAdapter sqlada = new SqlDataAdapter(comm);
                sqlada.Fill(dsData);

                // retrive the userId since it is auto incremented in the database and need to be added to the student table
                comm   = new SqlCommand("select userId from register where userName = '******'", cnn);
                sqlada = new SqlDataAdapter(comm);
                sqlada.Fill(dsData);


                foreach (DataRow row in dsData.Tables[0].Rows)
                {
                    userId = Int16.Parse(Convert.ToString(row["userId"]));
                }
            }
            catch (Exception e)
            {
                Student emptyStud = new Student();
                var     response  = Request.CreateResponse <Student>(System.Net.HttpStatusCode.BadRequest, emptyStud);
                cnn.Close();
                return(response);
            }

            using (var ctx = new ProjectEntities())
            {
                var user = ctx.registers.Where(x => x.userName == userName).Select(reg => new
                {
                    userId = reg.userId
                }).ToList();
                userId = Convert.ToInt32(user[0].userId);
                var         groupName = "defaultgroup" + userId;
                Group_table grpObj    = new Group_table();
                grpObj.group_name = groupName;
                grpObj.user_id    = userId;
                grpObj.status     = "Active";

                ctx.Group_table.Add(grpObj);
                ctx.SaveChanges();
                creategroupkey(groupName, userId, groupKey);
            }

            Student stud = new Student();

            stud.idStudent   = userName;
            stud.firstName   = firsName;
            stud.lastName    = lastName;
            stud.userId      = userId;
            stud.telephone   = telephone;
            stud.address     = address;
            stud.aliasMailId = aliasMailId;
            stud.emailId     = emailId;
            stud.skypeId     = skypeId;
            // now add to instructor table.
            try
            {
                SqlCommand comm = new SqlCommand("Insert into student(idStudent,firstName,lastName,userId,telephone,address,aliasMailId,emailId,skypeId) values('"
                                                 + stud.idStudent
                                                 + "','" + stud.firstName
                                                 + "','" + stud.lastName
                                                 + "','" + stud.userId
                                                 + "','" + stud.telephone
                                                 + "','" + stud.address
                                                 + "','" + stud.aliasMailId
                                                 + "','" + stud.emailId
                                                 + "','" + stud.skypeId
                                                 + "')", cnn);
                SqlDataAdapter sqlada = new SqlDataAdapter(comm);
                sqlada.Fill(dsData);
                cnn.Close();
                var response = Request.CreateResponse <Student>(System.Net.HttpStatusCode.Created, stud);
                return(response);
            }
            catch (Exception e)
            {
                var response = Request.CreateResponse <Student>(System.Net.HttpStatusCode.BadRequest, stud);
                cnn.Close();
                return(response);
            }
        }
Example #4
0
        public HttpResponseMessage CreateGroup([FromBody] Group objGrp)
        {
            long grpId = 0;

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (var ctx = new ProjectEntities())
                    {
                        Group_table grpObj = new Group_table();
                        grpObj.group_name = objGrp.groupName;
                        grpObj.user_id    = objGrp.userId;
                        grpObj.status     = "Active";

                        ctx.Group_table.Add(grpObj);
                        ctx.SaveChanges();

                        var user = ctx.Group_table.Where(x => x.group_name == objGrp.groupName).Select(grp => new
                        {
                            grpId = grp.group_id
                        }).ToList();

                        grpId = Convert.ToInt32(user[0].grpId);

                        Group_key gk = new Group_key();

                        gk.group_id   = grpId;
                        gk.group_key1 = objGrp.groupKey;
                        gk.status     = true;
                        gk.user_id    = objGrp.userId;
                        gk.version    = 1;
                        ctx.Group_key.Add(gk);
                        ctx.SaveChanges();
                    }
                    using (var db = new ProjectEntities())
                    {
                        for (int i = 0; i < objGrp.friend_ids.Count; i++)
                        {
                            Friend frnd = new Friend();
                            frnd.userId       = objGrp.userId;
                            frnd.GroupId      = grpId;
                            frnd.FriendUserId = objGrp.friend_ids[i];
                            db.Friends.Add(frnd);
                            db.SaveChanges();

                            var fr_id = Convert.ToInt32(objGrp.friend_ids[i]);

                            Friend userFrnd = new Friend();
                            userFrnd.FriendUserId = objGrp.userId;
                            userFrnd.GroupId      = grpId;
                            userFrnd.userId       = fr_id;
                            db.Friends.Add(userFrnd);
                            db.SaveChanges();


                            Group_key groupKey = new Group_key();
                            groupKey.group_id   = grpId;
                            groupKey.group_key1 = objGrp.group_keys[i];
                            groupKey.user_id    = fr_id;
                            groupKey.status     = true;
                            groupKey.version    = 1;
                            db.Group_key.Add(groupKey);
                            db.SaveChanges();
                        }
                    }
                    scope.Complete();
                    return(Request.CreateResponse <String>(System.Net.HttpStatusCode.Accepted, "Created"));
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse <Exception>(System.Net.HttpStatusCode.BadRequest, e));
            }
        }