public override Task <Projects_Reply> GetActiveProjects(Empty request, ServerCallContext context) { Projects_Reply Projects_Reply = new Projects_Reply(); //make Projects_Reply equal to a new Projects_Reply Projects_Reply.Error = "unknown error"; //set error to unknown error MySqlConnection connection = null; try { connection = CreateDBConnection(); var cmd = connection.CreateCommand(); //create command called cmd cmd.CommandText = "SELECT `projectId`, `name`, `active` FROM trackerdb.project WHERE `active` = 'Y';"; //fill cmd text var reader = cmd.ExecuteReader(); //execute cmd FillProject(Projects_Reply, reader); //calls func FillProjects } catch (Exception ex) //catch exception { Projects_Reply.Error = ex.Message; //if error } finally //after everything above is done { if (connection != null) //if connection is not null { if (connection.State != ConnectionState.Closed) //if connection is not closed { connection.Close(); //close connection } connection.Dispose(); //dipose of connection } } return(Task.FromResult(Projects_Reply)); //return projects }
public void GetProjects_Success() { //Arrange Projects_Reply reply = null; //Act if (channel == null) { channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); client = new Greeter.GreeterClient(channel); } try { reply = client.GetProjects(new Empty { }); Console.WriteLine($"Reply:\n{reply}"); } catch (Grpc.Core.RpcException ex) { Console.WriteLine($"ERROR:\n{ex.Message}\nNot connected to service."); } //Assert Assert.IsNotNull(reply.Projects); Assert.AreEqual(reply.Projects.ToString(), "[ { \"projectId\": 1, \"name\": \"Unity\", \"active\": \"Y\" }, { \"projectId\": 2, \"name\": \"Blender\", \"active\": \"Y\" }, { \"projectId\": 3, \"name\": \"Typing\", \"active\": \"Y\" }, { \"projectId\": 17, \"name\": \"Prodigy\", \"active\": \"Y\" }, { \"projectId\": 18, \"name\": \"Gaming\", \"active\": \"Y\" }, { \"projectId\": 19, \"name\": \"ASP.NET MVC\", \"active\": \"Y\" }, { \"projectId\": 20, \"name\": \"18 - Gaming\", \"active\": \"Y\" }, { \"projectId\": 21, \"name\": \"gamemaker\", \"active\": \"Y\" }, { \"projectId\": 22, \"name\": \"FreeCodeCamp\", \"active\": \"Y\" }, { \"projectId\": 23, \"name\": \"TestProject\", \"active\": \"Y\" }, { \"projectId\": 24, \"name\": \"Trumpet\", \"active\": \"Y\" }, { \"projectId\": 25, \"name\": \"24 - Trumpet\", \"active\": \"Y\" }, { \"projectId\": 26, \"name\": \"24 - Trumpet\", \"active\": \"Y\" }, { \"projectId\": 27, \"name\": \"Report\", \"active\": \"Y\" }, { \"projectId\": 28, \"name\": \"Report\", \"active\": \"Y\" }, { \"projectId\": 29, \"name\": \"Math\", \"active\": \"Y\" }, { \"projectId\": 30, \"name\": \"Math\", \"active\": \"Y\" } ]"); }
private static void FillProject(Projects_Reply Projects_Reply, MySqlDataReader reader) { while (reader.Read()) //read each project { var project = new Project() //create new Project { ProjectId = Convert.ToInt32(reader["projectId"]), //set projectId Name = Convert.ToString(reader["name"]), //set name Active = Convert.ToString(reader["active"]) //set active }; //Console.WriteLine(project.ProjectId);//write projectId to console Projects_Reply.Projects.Add(project); //add project to Projects } Projects_Reply.Error = ""; //set error equal to "" }