Exemple #1
0
        public async Task <IActionResult> GetWaterById(HttpRequest req, int waterId)
        {
            int patientId = await _authorizationService.GetUserId(req);

            if (patientId == 0)
            {
                return(new StatusCodeResult(StatusCodes.Status401Unauthorized));
            }

            WaterConsumptionModel waterConsumptionModel = await _waterRepository.GetSingleWaterConsumption(patientId, waterId);

            return(waterConsumptionModel != null
                ? (ActionResult) new OkObjectResult(waterConsumptionModel)
                : new StatusCodeResult(StatusCodes.Status204NoContent));
        }
Exemple #2
0
        public async Task <WaterConsumptionModel> InsertWaterConsumption(WaterConsumptionModel model, bool update)
        {
            using (SqlConnection sqlConnection = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection")))
            {
                string sqlStringCommand = "";
                if (update)
                {
                    sqlStringCommand =
                        $"UPDATE WaterConsumption " +
                        $"SET date = @DATE, amount = @AMOUNT, patient_id = @PATIENTID " +
                        $"OUTPUT Inserted.id " +
                        $"WHERE id = @ID";
                }
                else
                {
                    sqlStringCommand =
                        $"INSERT INTO WaterConsumption(date, amount, patient_id, weight_unit_id) " +
                        $"OUTPUT Inserted.id " +
                        $"VALUES(@DATE,@AMOUNT ,@PATIENTID, 7)";
                }

                await sqlConnection.OpenAsync();

                using (SqlCommand sqlCommand = new SqlCommand(sqlStringCommand, sqlConnection))
                {
                    sqlCommand.Parameters.Add("@PATIENTID", SqlDbType.Int).Value = model.PatientId;
                    sqlCommand.Parameters.Add("@DATE", SqlDbType.NVarChar).Value = model.Date.ToString("yyy-MM-dd");
                    sqlCommand.Parameters.Add("@AMOUNT", SqlDbType.Int).Value    = model.Amount;
                    if (update)
                    {
                        sqlCommand.Parameters.Add("@ID", SqlDbType.Int).Value = model.Id;
                    }

                    try
                    {
                        model.Id = (int)await sqlCommand.ExecuteScalarAsync();

                        sqlConnection.Close();
                    }
                    catch { return(null); }
                }
                return(model);
            }
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = (Routes.APIVersion + Routes.SingleWaterConsumption))] HttpRequest req,
            ILogger log, int waterId)
        {
            int patientId = await DIContainer.Instance.GetService <IAuthorization>().GetUserId(req);

            if (patientId == 0)
            {
                return(new StatusCodeResult(StatusCodes.Status401Unauthorized));
            }

            WaterConsumptionModel model    = new WaterConsumptionModel();
            IWaterRepository      waterRep = DIContainer.Instance.GetService <IWaterRepository>();

            try
            {
                var content    = await new StreamReader(req.Body).ReadToEndAsync();
                var jsonParsed = JObject.Parse(content);

                model = new WaterConsumptionModel()
                {
                    PatientId = patientId,
                    Amount    = (int)jsonParsed["amount"],
                    Id        = waterId
                };
                DateTime.TryParse(jsonParsed["date"].ToString(), out var date);
                if (date != null)
                {
                    model.Date = date;
                }
            }
            catch
            {
                return(new BadRequestObjectResult(Messages.ErrorPost));
            }

            model = await waterRep.InsertWaterConsumption(model, true);

            return(model != null
                ? (ActionResult) new OkObjectResult(model)
                : new BadRequestResult());
        }