public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } //We retrieve the userName field, which comes as a parameter to the function, by deserializing req.Content. string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string CompanyID = data.CompanyID; try { Branch[] allBranches = null; var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT * FROM BranchTable " + "WHERE companyID = @CompanyID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@CompanyID", CompanyID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { log.LogInformation("C# HTTP trigger function processed a request. 6"); var list = new List <Branch>(); while (oReader.Read()) { list.Add(new Branch { roomId = oReader["roomId"].ToString(), branchID = oReader["branchID"].ToString(), Name = oReader["Name"].ToString(), }); allBranches = list.ToArray(); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, allBranches)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } //We retrieve the userName field, which comes as a parameter to the function, by deserializing req.Content. string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string email = data.email; string Firstname = data.Firstname; string Lastname = data.Lastname; string Position = data.Position; string Password = data.Password; string branchID = data.branchID; string CompanyID = data.CompanyID; Password = SecurePasswordHasherHelper.Hash(Password); //If there is no username, we return the error message. try { //We get the Connection String in the Function App Settings section we defined. var str = Environment.GetEnvironmentVariable("sqldb_connection"); int modified; using (SqlConnection connection = new SqlConnection(str)) { string text = @"INSERT INTO Users (email, Firstname, Lastname, Position, branchID, PasswordHash, Role, CompanyID) " + "OUTPUT INSERTED.UserID " + "VALUES (@email, @Firstname, @Lastname, @Position, @branchID, @PasswordHash, @Role, @CompanyID);"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@email", email); command.Parameters.AddWithValue("@Firstname", Firstname); command.Parameters.AddWithValue("@Lastname", Lastname); command.Parameters.AddWithValue("@Position", Position); command.Parameters.AddWithValue("@branchID", branchID); command.Parameters.AddWithValue("@PasswordHash", Password); command.Parameters.AddWithValue("@Role", Role.Employee); command.Parameters.AddWithValue("@CompanyID", CompanyID); connection.Open(); modified = (int)command.ExecuteScalar(); connection.Close(); } return(req.CreateResponse(HttpStatusCode.OK, modified)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } log.LogInformation("C# HTTP trigger function processed a request."); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string UserID = data.UserID; try { Dictionary <string, RequestStatus> UserRequestList = new Dictionary <string, RequestStatus>(); var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT R.ShiftID " + "FROM Requests R " + "WHERE R.UserID = @UserID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@UserID", UserID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { while (oReader.Read()) { string ShiftID = oReader["ShiftID"].ToString(); UserRequestList.Add(ShiftID, new RequestStatus { status = true }); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, UserRequestList)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string UserID = data.UserID; User newUser = new User(); try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT email, Firstname, Lastname, Position FROM Users WHERE UserID = @UserID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@UserID", UserID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { while (oReader.Read()) { newUser.email = oReader["email"].ToString(); newUser.Firstname = oReader["Firstname"].ToString(); newUser.Lastname = oReader["Lastname"].ToString(); newUser.Position = oReader["Position"].ToString(); } connection.Close(); } } newUser.UserID = UserID; return(req.CreateResponse(HttpStatusCode.OK, newUser)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string shiftDate = data.shiftDate; string startTime = data.startTime; string endTime = data.endTime; string UserID = data.UserID; try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); int modified; using (SqlConnection connection = new SqlConnection(str)) { string text = @"INSERT INTO Shifts (shiftDate, startTime, endTime, UserID) " + "OUTPUT INSERTED.ShiftID " + "VALUES (@shiftDate, @startTime, @endTime, @UserID);"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@shiftDate", shiftDate); command.Parameters.AddWithValue("@startTime", startTime); command.Parameters.AddWithValue("@endTime", endTime); command.Parameters.AddWithValue("@UserID", UserID); connection.Open(); modified = (int)command.ExecuteScalar(); connection.Close(); } return(req.CreateResponse(HttpStatusCode.OK, modified)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } //We retrieve the userName field, which comes as a parameter to the function, by deserializing req.Content. string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string UserID = data.UserID; string ShiftID = data.ShiftID; //If there is no username, we return the error message. try { //We get the Connection String in the Function App Settings section we defined. var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"DELETE FROM Requests WHERE UserID = @UserID AND ShiftID = @ShiftID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@UserID", UserID); command.Parameters.AddWithValue("@ShiftID", ShiftID); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string Name = data.Name; string CompanyID = data.CompanyID; string roomId = data.roomId; try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); int modified; using (SqlConnection connection = new SqlConnection(str)) { string text = @"INSERT INTO BranchTable (companyID, Name, roomId) " + "OUTPUT INSERTED.branchID " + "VALUES (@CompanyID, @Name, @roomId);"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@CompanyID", CompanyID); command.Parameters.AddWithValue("@Name", Name); command.Parameters.AddWithValue("@roomId", roomId); connection.Open(); modified = (int)command.ExecuteScalar(); connection.Close(); } return(req.CreateResponse(HttpStatusCode.OK, modified)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string ShiftID = data.ShiftID; try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"DELETE FROM Shifts WHERE ShiftID=@ShiftID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@ShiftID", ShiftID); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string branchID = data.branchID; try { //We get the Connection String in the Function App Settings section we defined. var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"DELETE FROM BranchTable WHERE branchID=@branchID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@branchID", branchID); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } //We retrieve the userName field, which comes as a parameter to the function, by deserializing req.Content. string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string branchID = data.branchID; //If there is no username, we return the error message. try { User[] allUsers = null; //We get the Connection String in the Function App Settings section we defined. var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT UserID, email, Firstname, Lastname, Position FROM Users " + "WHERE branchID = @branchID AND Role NOT IN ('Owner', 'Manager')"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@branchID", branchID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { log.LogInformation("C# HTTP trigger function processed a request. 6"); var list = new List <User>(); while (oReader.Read()) { list.Add(new User { UserID = oReader["UserID"].ToString(), email = oReader["email"].ToString(), Firstname = oReader["Firstname"].ToString(), Lastname = oReader["Lastname"].ToString(), Position = oReader["Position"].ToString() }); allUsers = list.ToArray(); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, allUsers)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string ShiftID = data.ShiftID; string UserID = data.UserID; string Comment = data.Comment; Boolean Urgent = data.Urgent; string Version = data.Version; try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { connection.Open(); SqlTransaction sqlTran = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = sqlTran; try { command.CommandText = "SELECT COUNT(*) FROM Shifts WHERE ShiftID = @ShiftID;"; command.Parameters.AddWithValue("@ShiftID", ShiftID); Int32 numRows = (Int32)command.ExecuteScalar(); if (numRows <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Shift no longer exists!")); } command.CommandText = "SELECT COUNT(*) FROM Shifts WHERE ShiftID = @ShiftID2 AND Ver = CONVERT(VARBINARY, @Version, 1);"; command.Parameters.AddWithValue("@ShiftID2", ShiftID); command.Parameters.AddWithValue("@Version", Version); Int32 numRowsVer = (Int32)command.ExecuteScalar(); if (numRowsVer <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Shift was updated before adding.")); } // Inserts the request. command.CommandText = @"INSERT INTO Requests (ShiftID, UserID, Comment, Urgent) " + "VALUES (@ShiftID3, @UserID, @Comment, @Urgent)"; command.Parameters.AddWithValue("@ShiftID3", ShiftID); command.Parameters.AddWithValue("@UserID", UserID); command.Parameters.AddWithValue("@Comment", Comment); command.Parameters.AddWithValue("@Urgent", Urgent); int countUpdated = command.ExecuteNonQuery(); if (countUpdated <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Shift was already updated!")); } sqlTran.Commit(); connection.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); try { // Attempt to roll back the transaction. sqlTran.Rollback(); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback!")); } catch (Exception exRollback) { // Throws an InvalidOperationException if the connection // is closed or the transaction has already been rolled // back on the server. Console.WriteLine(exRollback.Message); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback exception!")); } } } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string CompanyID = data.CompanyID; try { Manager[] allManagers = null; var str = Environment.GetEnvironmentVariable("sqldb_connection"); Dictionary <string, List <Manager> > ManagerDict = new Dictionary <string, List <Manager> >(); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT U.UserID, U.Firstname, U.Lastname, U.email, U.branchID " + "FROM Users U " + "WHERE U.CompanyID = @CompanyID AND U.Role = 'Manager'"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@CompanyID", CompanyID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { var list = new List <Manager>(); while (oReader.Read()) { Manager temp = new Manager { UserID = oReader["UserID"].ToString(), Firstname = oReader["Firstname"].ToString(), Lastname = oReader["Firstname"].ToString(), email = oReader["email"].ToString(), branchID = oReader["branchID"].ToString() }; if (!ManagerDict.ContainsKey(temp.branchID)) { ManagerDict.Add(temp.branchID, new List <Manager>()); } ManagerDict[temp.branchID].Add(temp); list.Add(temp); allManagers = list.ToArray(); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, ManagerDict)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string ShiftID = data.ShiftID; string AddUserID = data.AddUserID; string DelUserID = data.DelUserID; string Version = data.Version; try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { connection.Open(); SqlTransaction sqlTran = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = sqlTran; try { // UPDATE UserID for the Shift, to the person who accepted the request command.CommandText = @"UPDATE Shifts " + "SET UserID = @AddUserID " + "WHERE ShiftID = @ShiftID2 AND Ver = CONVERT(VARBINARY, @Version, 1);"; command.Parameters.AddWithValue("@ShiftID2", ShiftID); command.Parameters.AddWithValue("@AddUserID", AddUserID); command.Parameters.AddWithValue("@Version", Version); int countUpdated = command.ExecuteNonQuery(); if (countUpdated <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Shift was already updated!")); } // REMOVE REQUEST FROM TABLE command.CommandText = @"DELETE FROM Requests WHERE ShiftID = @ShiftID3 AND UserID = @DelUserID;"; command.Parameters.AddWithValue("@ShiftID3", ShiftID); command.Parameters.AddWithValue("@DelUserID", DelUserID); int countDeleted = command.ExecuteNonQuery(); if (countDeleted <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Request no longer exists!")); } sqlTran.Commit(); connection.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); try { // Attempt to roll back the transaction. sqlTran.Rollback(); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback!")); } catch (Exception exRollback) { // Throws an InvalidOperationException if the connection // is closed or the transaction has already been rolled // back on the server. Console.WriteLine(exRollback.Message); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback exception!")); } } } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string ShiftID = data.ShiftID; string shiftDate = data.shiftDate; string startTime = data.startTime; string endTime = data.endTime; string Version = data.Version; try { var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { connection.Open(); SqlTransaction sqlTran = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = sqlTran; try { command.CommandText = "SELECT COUNT(*) FROM Shifts WHERE ShiftID = @ShiftID;"; command.Parameters.AddWithValue("@ShiftID", ShiftID); Int32 numRows = (Int32)command.ExecuteScalar(); if (numRows <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Item has already been deleted!")); } command.CommandText = @"UPDATE Shifts " + "SET shiftDate = @shiftDate, startTime = @startTime, endTime = @endTime " + "WHERE ShiftID = @ShiftID2 AND Ver = CONVERT(VARBINARY, @Version, 1);"; command.Parameters.AddWithValue("@ShiftID2", ShiftID); command.Parameters.AddWithValue("@shiftDate", shiftDate); command.Parameters.AddWithValue("@startTime", startTime); command.Parameters.AddWithValue("@endTime", endTime); command.Parameters.AddWithValue("@Version", Version); int count = command.ExecuteNonQuery(); if (count <= 0) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Item was already updated!")); } sqlTran.Commit(); connection.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); try { // Attempt to roll back the transaction. sqlTran.Rollback(); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback!")); } catch (Exception exRollback) { // Throws an InvalidOperationException if the connection // is closed or the transaction has already been rolled // back on the server. Console.WriteLine(exRollback.Message); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback exception!")); } } } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee, Role.Manager, Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } //We retrieve the userName field, which comes as a parameter to the function, by deserializing req.Content. string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string email = data.email; string oldPass = data.oldPassword; string newPass = data.newPassword; newPass = SecurePasswordHasherHelper.Hash(newPass); try { //We get the Connection String in the Function App Settings section we defined. var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { connection.Open(); SqlTransaction sqlTran = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = sqlTran; try { command.CommandText = @"SELECT PasswordHash FROM Users WHERE email = @email"; command.Parameters.AddWithValue("@email", email); string verifyPass = ""; using (SqlDataReader oReader = command.ExecuteReader()) { while (oReader.Read()) { verifyPass = oReader["PasswordHash"].ToString(); } } if (!SecurePasswordHasherHelper.Verify(oldPass, verifyPass)) { return(req.CreateResponse(HttpStatusCode.NotAcceptable, "Incorrect Password!")); } command.CommandText = @"UPDATE Users " + "SET PasswordHash = @NewPassword " + "WHERE email = @email"; command.Parameters.AddWithValue("@email", email); command.Parameters.AddWithValue("@NewPassword", newPass); command.ExecuteNonQuery(); sqlTran.Commit(); connection.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); try { // Attempt to roll back the transaction. sqlTran.Rollback(); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback!")); } catch (Exception exRollback) { // Throws an InvalidOperationException if the connection // is closed or the transaction has already been rolled // back on the server. Console.WriteLine(exRollback.Message); return(req.CreateResponse(HttpStatusCode.BadRequest, "Rollback exception!")); } } } return(req.CreateResponse(HttpStatusCode.OK, "Success")); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee, Role.Manager, Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string shiftDate = data.shiftDate; string branchID = data.branchID; try { Request[] allRequests = null; var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT R.UserID, R.ShiftID, R.Comment, R.Urgent, U.Firstname, U.email, U.Position, S.startTime, S.endTime, S.Ver " + "FROM Requests R " + "INNER JOIN Users U ON R.UserID = U.UserID " + "INNER JOIN Shifts S ON R.ShiftID = S.ShiftID " + "WHERE S.shiftDate = @shiftDate AND U.branchID = @branchID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@shiftDate", shiftDate); command.Parameters.AddWithValue("@branchID", branchID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { log.LogInformation("C# HTTP trigger function processed a request. 6"); var list = new List <Request>(); while (oReader.Read()) { ulong dbrow = BitConverter.ToUInt64((byte[])oReader["Ver"], 0); log.LogInformation(dbrow.ToString()); string temp = BitConverter.ToString(BitConverter.GetBytes(dbrow).ToArray()).Replace("-", ""); log.LogInformation(string.Format("0x{0:X}", temp)); list.Add(new Request { UserID = oReader["UserID"].ToString(), ShiftID = oReader["ShiftID"].ToString(), Comment = oReader["Comment"].ToString(), Urgent = (bool)oReader["Urgent"], email = oReader["email"].ToString(), Firstname = oReader["Firstname"].ToString(), Position = oReader["Position"].ToString(), startTime = oReader["startTime"].ToString(), endTime = oReader["endTime"].ToString(), Version = string.Format("0x{0:X}", temp) }); allRequests = list.ToArray(); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, allRequests)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee, Role.Manager })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string UserID = data.UserID; log.LogInformation(UserID); try { User[] allShifts = null; var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT * FROM Shifts " + "WHERE UserID = @UserID"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@UserID", UserID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { log.LogInformation("C# HTTP trigger function processed a request. 6"); var list = new List <User>(); while (oReader.Read()) { ulong dbrow = BitConverter.ToUInt64((byte[])oReader["Ver"], 0); log.LogInformation(dbrow.ToString()); string temp = BitConverter.ToString(BitConverter.GetBytes(dbrow).ToArray()).Replace("-", ""); log.LogInformation(string.Format("0x{0:X}", temp)); list.Add(new User { ShiftID = oReader["ShiftID"].ToString(), shiftDate = oReader["shiftDate"].ToString(), startTime = oReader["startTime"].ToString(), endTime = oReader["endTime"].ToString(), Version = string.Format("0x{0:X}", temp) }); allShifts = list.ToArray(); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, allShifts)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }
public static async Task <object> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string token = req.Headers.Authorization.ToString(); if (!AuthorizationHelper.Authorized(token, new string[] { Role.Employee, Role.Manager, Role.Owner })) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Unauthorized!")); } string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); string branchID = data.branchID; try { Dictionary <string, RequestCounts> RequestCountList = new Dictionary <string, RequestCounts>(); var str = Environment.GetEnvironmentVariable("sqldb_connection"); using (SqlConnection connection = new SqlConnection(str)) { string text = @"SELECT S.shiftDate, COUNT(*) AS NumRequests " + "FROM Shifts S " + "JOIN Requests R ON R.ShiftID = S.ShiftID " + "JOIN Users U ON U.UserID = R.UserID " + "WHERE U.branchID = @branchID " + "GROUP BY S.shiftDate"; SqlCommand command = new SqlCommand(text, connection); command.Parameters.AddWithValue("@branchID", branchID); connection.Open(); using (SqlDataReader oReader = command.ExecuteReader()) { log.LogInformation("C# HTTP trigger function processed a request. 6"); while (oReader.Read()) { string date = oReader["shiftDate"].ToString().Substring(0, 10); if (date[9] != ' ') { date = date.Substring(0, 8); } date = date.Trim(); RequestCountList.Add(date, new RequestCounts { Count = (int)oReader["NumRequests"], }); } connection.Close(); } } return(req.CreateResponse(HttpStatusCode.OK, RequestCountList)); } catch { return(req.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong!")); } }