public HttpResponseMessage POST(FriendShip friendEmail)
        {
            if (friendEmail.Friend == "" || friendEmail.User == "")
            {
                return(new HttpResponseMessage(HttpStatusCode.PaymentRequired));
            }
            SQLBlock block = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"INSERT INTO [dbo].[friends] (email, friendEmail) VALUES ('{friendEmail.User}', '{friendEmail.Friend}');", connection))
                {
                    try
                    {
                        connection.Open();
                        command.BeginExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public HttpResponseMessage RemoveFriend(FriendShip friendEmail)
        {
            if (friendEmail.Friend == "" || friendEmail.User == "")
            {
                return(new HttpResponseMessage(HttpStatusCode.PaymentRequired));
            }
            SQLBlock block = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"DELETE FROM [dbo].[friends] WHERE email ='{friendEmail.User}' AND friendEmail = '{friendEmail.Friend}';", connection))
                {
                    try
                    {
                        connection.Open();
                        command.BeginExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public HttpResponseMessage POST(User theUser)
        {
            SQLBlock      block   = new SQLBlock();
            List <string> friends = new List <string>();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select friendEmail FROM [dbo].[friends] WHERE email = '{theUser.Username}'", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    friends.Add(reader[0].ToString());
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }

            var response = Request.CreateResponse(HttpStatusCode.OK, friends.ToArray());

            return(response);
        }
        public HttpResponseMessage POST(Tag tag)
        {
            List <string> PIDS  = new List <string>();
            SQLBlock      block = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"Select PID from [dbo].[tags] WHERE tag = '{tag.TagName}'", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    PIDS.Add(reader[0].ToString());
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }
            var response = Request.CreateResponse(HttpStatusCode.OK, PIDS.ToArray());

            return(response);
        }
        public HttpResponseMessage GET(string id)
        {
            string PID = id;
            HttpResponseMessage message  = new HttpResponseMessage(HttpStatusCode.OK);
            SQLBlock            block    = new SQLBlock();
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand cmd = new SqlCommand($"Select photoData from [dbo].[Photos] where PID = '{PID}';", connection))
                {
                    try
                    {
                        connection.Open();
                        byte[] image = (byte[])cmd.ExecuteScalar();
                        response.Content = new ByteArrayContent(image);
                        connection.Close();
                    }
                    catch (Exception e)
                    {
                        Console.Write(e);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }
            string extension;
            string caption;

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand cmd2 = new SqlCommand($"Select caption, extension from [dbo].[Photos] where PID = '{PID}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = cmd2.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                caption   = reader["caption"].ToString();
                                extension = reader["extension"].ToString();
                                response.Content.Headers.ContentType = new MediaTypeHeaderValue(extension);
                            }
                        }
                    }catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }
            response.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue()
            {
                Public = true,
                MaxAge = new TimeSpan(365, 0, 0, 0)
            };
            return(response);
        }
        public HttpResponseMessage POST(Delete albumUser)
        {
            string   ID    = "";
            SQLBlock block = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getID = new SqlCommand($"select ID from [dbo].[Users] where email = '{albumUser.User}';", connection))
                {
                    //Now we have the USERID
                    try
                    {
                        connection.Open();
                        using (SqlDataReader readers = getID.ExecuteReader())
                        {
                            if (readers.HasRows)
                            {
                                while (readers.Read())
                                {
                                    ID = readers[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand deleteAlbum = new SqlCommand($"DELETE FROM [dbo].[Albums] where title = '{albumUser.Album}' AND userID = '{ID}';", connection))
                {
                    try
                    {
                        connection.Open();
                        deleteAlbum.BeginExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public HttpResponseMessage GET()
        {
            List <string> firstNames = new List <string>();
            List <string> lastNames  = new List <string>();
            List <string> emails     = new List <string>();
            SQLBlock      block      = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
            {
                using (SqlCommand command = new SqlCommand($"SELECT TOP(10) [firstname], [lastname], [email] FROM [dbo].[Users]", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    firstNames.Add(reader[0].ToString());
                                    lastNames.Add(reader[1].ToString());
                                    emails.Add(reader[2].ToString());
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }
            }
            JArray responseObjects = new JArray();

            for (int i = 0; i < emails.Count; i++)
            {
                JObject J = new JObject(
                    new JProperty("firstName", firstNames[i]),
                    new JProperty("lastName", lastNames[i]),
                    new JProperty("email", emails[i]));
                responseObjects.Add(J);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK, responseObjects);

            return(response);
        }
Beispiel #8
0
        public HttpResponseMessage POST(Comment comment)
        {
            SQLBlock block = new SQLBlock();
            string   ID    = "";

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getID = new SqlCommand($"select ID from [dbo].[Users] where email = '{comment.User}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader readers = getID.ExecuteReader())
                        {
                            if (readers.HasRows)
                            {
                                while (readers.Read())
                                {
                                    ID = readers[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"INSERT INTO [dbo].[Comments] (PID, ID, comment) VALUES ('{comment.Photo}', '{ID}', '{comment.Text}')", connection))
                {
                    try
                    {
                        connection.Open();
                        command.BeginExecuteNonQuery();
                    }
                    catch (SqlException e)
                    {
                        connection.Close();
                        Console.Write(e.Message);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict)); // bad formed request
                    }
                }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public HttpResponseMessage Get()
        {
            List <string> tagName   = new List <string>();
            List <string> numCounts = new List <string>();
            SQLBlock      block     = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"Select Top(10) tag, COUNT(*) AS num FROM [dbo].[tags] Group BY tag ORDER BY num desc", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    tagName.Add(reader[0].ToString());
                                    numCounts.Add(reader[1].ToString());
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }
            JArray responseObjects = new JArray();

            for (int i = 0; i < tagName.Count; i++)
            {
                JObject J = new JObject(
                    new JProperty("tagName", tagName[i]),
                    new JProperty("count", numCounts[i]));
                responseObjects.Add(J);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK, responseObjects);

            return(response);
        }
Beispiel #10
0
        public HttpResponseMessage POST(Like like)
        {
            SQLBlock block = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"INSERT INTO [dbo].[Likes] (PID, email) VALUES ('{like.Photo}', '{like.User}')", connection))
                {
                    try
                    {
                        connection.Open();
                        command.BeginExecuteNonQuery();
                    }
                    catch (SqlException e)
                    {
                        connection.Close();
                        Console.Write(e.Message);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict)); // bad formed request
                    }
                }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public HttpResponseMessage POST(SearchTerm search)
        {
            var           query   = search.Query;
            SQLBlock      block   = new SQLBlock();
            List <string> friends = new List <string>();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select firstname, lastname, email FROM [dbo].[Users] WHERE email like '%{query}%' OR firstname like '%{query}%' OR lastname like '%{query}%' ", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    string dataRow = "";
                                    dataRow += reader["firstname"].ToString() + " :";
                                    dataRow += reader["lastname"].ToString() + " :";
                                    dataRow += reader["email"].ToString();
                                    friends.Add(dataRow);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            var response = Request.CreateResponse(HttpStatusCode.OK, friends.ToArray());

            return(response);
        }
        public HttpResponseMessage getPhotoIDs()
        {
            List <string> friendPIDS = new List <string>();
            SQLBlock      block      = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select  top 50 percent PID from Photos order by newid()", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    friendPIDS.Add(reader[0].ToString());
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }



            // select top 10 percent * from Photos order by newid()
            var response = Request.CreateResponse(HttpStatusCode.OK, friendPIDS.ToArray());

            return(response);
        }
        public HttpResponseMessage POST(PID photo)
        {
            SQLBlock block = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand deletePhoto = new SqlCommand($"DELETE FROM [dbo].[Photos] where PID = '{photo.PhotoID}';", connection))
                {
                    try
                    {
                        connection.Open();
                        deletePhoto.BeginExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public object POST(User user)
        {
            List <string> entries = new List <string>();
            string        ID      = "";
            SQLBlock      block   = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getID = new SqlCommand($"select ID from [dbo].[Users] where email = '{user.Username}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = getID.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    ID = reader[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getAlbums = new SqlCommand($"select AID, title from [dbo].[Albums] where userID = '{ID}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = getAlbums.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    AlbumEntry entry = new AlbumEntry(reader["AID"].ToString(), reader["title"].ToString());
                                    entries.Add(entry.ToString());
                                }
                            }
                            else
                            {
                                return(new object());
                            }
                            connection.Close();
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }

            return(entries);
        }
        public HttpResponseMessage POST(Photo image)
        {
            SQLBlock block = new SQLBlock();
            string   ID    = "";
            string   AID   = "";

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getID = new SqlCommand($"select ID from [dbo].[Users] where email = '{image.user}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader readers = getID.ExecuteReader())
                        {
                            if (readers.HasRows)
                            {
                                while (readers.Read())
                                {
                                    ID = readers[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select AID from [dbo].[Albums] where title = '{image.albumName}' AND userId = '{ID}'", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader2 = command.ExecuteReader())
                        {
                            if (reader2.HasRows)
                            {
                                while (reader2.Read())
                                {
                                    AID = reader2[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand insert = new SqlCommand($"INSERT INTO [dbo].[Photos] (photoData, caption, size, extension, AID) values(@binaryValue, '{image.caption}', '{image.size}', '{image.type}', '{AID}')", connection))
                {
                    try
                    {
                        connection.Open();
                        insert.Parameters.Add("@binaryValue", SqlDbType.VarBinary, -1).Value = image.photo.ToArray();
                        insert.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            string photoNumber = "";

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand count = new SqlCommand($"select TOP (1) PID from [dbo].[Photos] ORDER BY PID DESC", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader3 = count.ExecuteReader())
                        {
                            if (reader3.HasRows)
                            {
                                while (reader3.Read())
                                {
                                    photoNumber = reader3[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            string        myTags  = image.tags.Replace(" ", "");
            List <string> tagList = myTags.Split('#').ToList();
            string        query   = "INSERT INTO [dbo].[tags] (tag, PID) VALUES ";

            foreach (var item in tagList)
            {
                query += $"('{item}','{photoNumber}'),";
            }
            query = query.Remove(query.Length - 1);
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand insert2 = new SqlCommand(query, connection))
                {
                    try
                    {
                        connection.Open();
                        insert2.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }



            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        public HttpResponseMessage getPhotosPerAlbum()
        {
            string   albumName = Request.Headers.GetValues("album").First().ToString();
            string   user      = Request.Headers.GetValues("user").First().ToString();
            SQLBlock block     = new SQLBlock();
            string   ID        = "";
            string   AID       = "";

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getID = new SqlCommand($"select ID from [dbo].[Users] where email = '{user}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader readers = getID.ExecuteReader())
                        {
                            if (readers.HasRows)
                            {
                                while (readers.Read())
                                {
                                    ID = readers[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select AID from [dbo].[Albums] where title = '{albumName}' AND userId = '{ID}'", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader2 = command.ExecuteReader())
                        {
                            if (reader2.HasRows)
                            {
                                while (reader2.Read())
                                {
                                    AID = reader2[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    connection.Close();
                }
            List <string> AIDs = new List <string>();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand cmd = new SqlCommand($"Select PID, caption from [dbo].[photos] where AID = '{AID}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                AIDs.Add(reader[0].ToString());
                                AIDs.Add(reader[1].ToString());
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e);
                        connection.Close();
                    }
                }
            return(Request.CreateResponse(HttpStatusCode.OK, AIDs.ToArray()));
        }
        public HttpResponseMessage Post(Album album)
        {
            string   ID      = "";
            string   myAlbum = album.AlbumName;
            SQLBlock block   = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand getID = new SqlCommand($"select ID from [dbo].[Users] where email = '{album.User}';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = getID.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    ID = reader[0].ToString();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select title from [dbo].[Albums] where title = '{album.AlbumName}' AND userID = '{ID}';", connection))
                {
                    try
                    {
                        connection.Open();
                    }
                    catch (Exception e)
                    {
                        connection.Close();
                        Console.Write(e);
                        return(new HttpResponseMessage(HttpStatusCode.Conflict));
                    }
                    try
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                connection.Close();
                                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
                            }
                        }
                        string insertQuery = $"INSERT INTO [dbo].[Albums] (title, userID, dateCreated) " +
                                             $"values ('{album.AlbumName}','{ID}', '{DateTime.Now}');";
                        using (SqlCommand insertCommand = new SqlCommand(insertQuery, connection))
                        {
                            try
                            {
                                insertCommand.ExecuteNonQuery();
                            }
                            catch (Exception e2)
                            {
                                Console.Write(e2.Message);
                                connection.Close();
                                return(new HttpResponseMessage(HttpStatusCode.Conflict));
                            }
                        }
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.OK));
                    }
                    catch (SqlException e3)
                    {
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict)); // bad formed request
                    }
                }
        }
Beispiel #18
0
        public HttpResponseMessage GET()
        {
            List <string> PID      = new List <string>();
            List <string> numLikes = new List <string>();
            List <string> comments = new List <string>();
            SQLBlock      block    = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand command = new SqlCommand($"select TOP (10) PID from [dbo].[Photos]", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    PID.Add(reader[0].ToString());
                                }
                            }
                        }
                    }
                    catch (SqlException e3)
                    {
                        connection.Close();
                        return(new HttpResponseMessage(HttpStatusCode.Conflict)); // bad formed request
                    }
                }
            foreach (var value in PID)
            {
                //Get The number of likes for each PID
                using (SqlConnection connection = new SqlConnection(block.connectionString))
                    using (SqlCommand command = new SqlCommand($"SELECT Count(*) FROM LIKES L WHERE L.PID = '{value}'", connection))
                    {
                        try
                        {
                            connection.Open();
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    while (reader.Read())
                                    {
                                        numLikes.Add(reader[0].ToString());
                                    }
                                }
                            }
                        }
                        catch (SqlException e3)
                        {
                            connection.Close();
                            return(new HttpResponseMessage(HttpStatusCode.Conflict)); // bad formed request
                        }
                    }
                //Get the comments for each PID
                using (SqlConnection connection = new SqlConnection(block.connectionString))
                    using (SqlCommand command = new SqlCommand($"SELECT comment FROM Comments C WHERE C.PID = '{value}'", connection))
                    {
                        try
                        {
                            connection.Open();
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                string myComments = "";
                                if (reader.HasRows)
                                {
                                    while (reader.Read())
                                    {
                                        myComments += reader[0].ToString() + ";";
                                    }
                                }
                                comments.Add(myComments);
                            }
                        }
                        catch (SqlException e3)
                        {
                            connection.Close();
                            return(new HttpResponseMessage(HttpStatusCode.Conflict)); // bad formed request
                        }
                    }
            }
            JArray responseObjects = new JArray();

            for (int i = 0; i < PID.Count; i++)
            {
                JObject J = new JObject(
                    new JProperty("PID", PID[i]),
                    new JProperty("numLikes", numLikes[i]),
                    new JProperty("comments", comments[i]));
                responseObjects.Add(J);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK, responseObjects);

            return(response);
        }
Beispiel #19
0
        public HttpResponseMessage POST(Search query)
        {
            List <string> comments = new List <string>();
            List <string> tags     = new List <string>();
            List <string> photos   = new List <string>();
            SQLBlock      block    = new SQLBlock();

            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand cmd = new SqlCommand($"Select PID from [dbo].[Photos] where caption like '%{query.Query}%';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                photos.Add(reader[0].ToString());
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e);
                        connection.Close();
                    }
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand cmd = new SqlCommand($"Select PID from [dbo].[Comments] where comment like '%{query.Query}%';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                comments.Add(reader[0].ToString());
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e);
                        connection.Close();
                    }
                }
            using (SqlConnection connection = new SqlConnection(block.connectionString))
                using (SqlCommand cmd = new SqlCommand($"Select PID from [dbo].[Tags] where tag like '%{query.Query}%';", connection))
                {
                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                tags.Add(reader[0].ToString());
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e);
                        connection.Close();
                    }
                }

            List <List <string> > myArray = new List <List <string> >();

            myArray.Add(comments);
            myArray.Add(tags);
            myArray.Add(photos);
            var response = Request.CreateResponse(HttpStatusCode.OK, myArray);

            if (response == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
            return(response);
        }