public static async Task <IActionResult> CreateDieataryManagement( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = (Routes.APIVersion + Routes.DietaryManagement))] HttpRequest req, ILogger log) { //link voor swagger https://devkimchi.com/2019/02/02/introducing-swagger-ui-on-azure-functions/ log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); if (string.IsNullOrEmpty(requestBody)) { return(new UnprocessableEntityObjectResult(Messages.ErrorMissingValues)); } DietaryManagementModel dietary = new DietaryManagementModel(); JsonConvert.PopulateObject(requestBody, dietary); #region AuthCheck AuthResultModel authResult = await DIContainer.Instance.GetService <IAuthorization>().CheckAuthorization(req, dietary.PatientId); if (!authResult.Result) { return(new StatusCodeResult((int)authResult.StatusCode)); } #endregion Dictionary <ServiceDictionaryKey, object> dictionary = await DIContainer.Instance.GetService <IDietaryManagementService>().TryAddDietaryManagement(dietary); return(DIContainer.Instance.GetService <IResponseHandler>().ForgeResponse(dictionary)); }
public async Task <bool> UpdateDietaryManagement(int id, DietaryManagementModel dietaryManagement) { bool succes; using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection"))) { conn.Open(); var query = @"UPDATE DietaryManagement SET dietary_restriction_id = (SELECT r.id FROM DietaryRestriction as r WHERE r.description = @DESCRIPTION) ,amount = @AMOUNT ,patient_id = @PATIENT ,is_active = @ISACTIVE WHERE id = @ID"; SqlCommand command = new SqlCommand(query, conn); command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = dietaryManagement.Description; command.Parameters.Add("@AMOUNT", SqlDbType.Int).Value = dietaryManagement.Amount; command.Parameters.Add("@PATIENT", SqlDbType.Int).Value = dietaryManagement.PatientId; command.Parameters.Add("@ISACTIVE", SqlDbType.Bit).Value = dietaryManagement.IsActive; command.Parameters.Add("@ID", SqlDbType.Int).Value = id; succes = await command.ExecuteNonQueryAsync() > 0; } return(succes); }
public async Task <bool> AddDietaryManagement(DietaryManagementModel dietary) { using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection"))) { conn.Open(); var query = @"INSERT INTO DietaryManagement (dietary_restriction_id, amount, patient_id, is_active) VALUES ( ( SELECT r.id FROM DietaryRestriction as r WHERE r.description = @DESCRIPTION ), @AMOUNT, @PATIENT, @ISACTIVE )"; bool succes; try { SqlCommand command = new SqlCommand(query, conn); command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = dietary.Description; command.Parameters.Add("@AMOUNT", SqlDbType.Int).Value = dietary.Amount; command.Parameters.Add("@PATIENT", SqlDbType.Int).Value = dietary.PatientId; command.Parameters.Add("@ISACTIVE", SqlDbType.Bit).Value = dietary.IsActive; succes = await command.ExecuteNonQueryAsync() > 0; } catch (Exception e) { throw e; } return(succes); } }
public async Task <List <DietaryManagementModel> > GetDietaryManagementByPatientAsync(int patientId) { List <DietaryManagementModel> dietaryManagementModels = new List <DietaryManagementModel>(); using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection"))) { conn.Open(); var text = $"SELECT b.description, a.amount, a.is_active, a.patient_id, a.id " + $"FROM DietaryManagement AS a, DietaryRestriction AS b " + $"WHERE a.dietary_restriction_id = b.id AND a.patient_id={patientId}"; using (SqlCommand cmd = new SqlCommand(text, conn)) { try { SqlDataReader reader = await cmd.ExecuteReaderAsync(); while (reader.Read()) { DietaryManagementModel dietaryManagementModel = new DietaryManagementModel(); // Uit lezen bijv dietaryManagementModel.Id = Int32.Parse(reader["id"].ToString()); dietaryManagementModel.Description = reader["description"].ToString(); dietaryManagementModel.Amount = Int32.Parse(reader["amount"].ToString()); dietaryManagementModel.PatientId = Int32.Parse(reader["patient_id"].ToString()); dietaryManagementModel.IsActive = Convert.ToBoolean(reader["is_active"]); dietaryManagementModels.Add(dietaryManagementModel); } } catch (Exception e) { throw e; } } } return(dietaryManagementModels); }
public static async Task <IActionResult> UpdateDietaryManagement( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = (Routes.APIVersion + Routes.DietaryManagementById))] HttpRequest req, int dietId, ILogger log) { //link voor swagger https://medium.com/@yuka1984/open-api-swagger-and-swagger-ui-on-azure-functions-v2-c-a4a460b34b55 log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); DietaryManagementModel dietary = new DietaryManagementModel(); JsonConvert.PopulateObject(requestBody, dietary); AuthResultModel authResult = await DIContainer.Instance.GetService <IAuthorization>().CheckAuthorization(req, dietary.PatientId); if (!authResult.Result) { return(new StatusCodeResult((int)authResult.StatusCode)); } Dictionary <ServiceDictionaryKey, object> dictionary = await DIContainer.Instance.GetService <IDietaryManagementService>().TryUpdateDietaryManagement(dietId, dietary); return(DIContainer.Instance.GetService <IResponseHandler>().ForgeResponse(dictionary)); }
public async Task <Dictionary <ServiceDictionaryKey, object> > TryAddDietaryManagement(DietaryManagementModel dietary) { Dictionary <ServiceDictionaryKey, object> dictionary = new Dictionary <ServiceDictionaryKey, object>(); try { bool susces = await _dietaryManagmentRepository.AddDietaryManagement(dietary); dynamic data = _messageSerializer.Serialize(susces); dictionary.Add(ServiceDictionaryKey.VALUE, data); } catch (Exception ex) { dictionary.AddErrorMessage(ServiceDictionaryKey.ERROR, ex, FeedbackHandler); } return(dictionary); }