Ejemplo n.º 1
0
        public async Task <ActionResult> GetConsumptionByConsumptionId(HttpRequest req, string consumptionId)
        {
            if (!int.TryParse(consumptionId, out int id))
            {
                return(new BadRequestObjectResult(Messages.ErrorIncorrectId));
            }

            ConsumptionView targetConsumption = await consumptionRepository.GetConsumptionByConsumptionId(id);

            // Auth check
            if (!await Authorised(req, targetConsumption.PatientId, true))
            {
                return(new BadRequestObjectResult(Messages.AuthNoAcces));
            }

            var consumptionJson = JsonConvert.SerializeObject(targetConsumption);

            return(consumptionJson != null && targetConsumption.ConsumptionId != 0
                ? (ActionResult) new OkObjectResult(consumptionJson)
                : new BadRequestObjectResult(Messages.ErrorIncorrectId));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> UpdateConsumption(HttpRequest req, string consumptionId)
        {
            if (!int.TryParse(consumptionId, out int Id))
            {
                return(new BadRequestObjectResult(Messages.ErrorIncorrectId));
            }

            ConsumptionView consumption = await consumptionRepository.GetConsumptionByConsumptionId(Id);

            int targetPatientId = consumption.PatientId;

            ConsumptionInput updateConsumption = new ConsumptionInput();
            string           consumptionJson   = await new StreamReader(req.Body).ReadToEndAsync();

            JsonConvert.PopulateObject(consumptionJson, updateConsumption);

            if (!CorrectConsumption(updateConsumption))
            {
                return(new BadRequestObjectResult(Messages.ErrorInvalidConsumptionObject));
            }

            // Check if updated consumption patientId equals target patientId
            if (updateConsumption.PatientId != targetPatientId)
            {
                return(new BadRequestObjectResult(Messages.ErrorPut));
            }

            // Auth check
            if (!await Authorised(req, targetPatientId, false))
            {
                return(new BadRequestObjectResult(Messages.AuthNoAcces));
            }

            if (await consumptionRepository.UpdateConsumption(Id, updateConsumption))
            {
                return(new OkObjectResult(Messages.OKUpdate));
            }
            return(new BadRequestObjectResult(Messages.ErrorPut));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> RemoveConsumption(HttpRequest req, string consumptionId)
        {
            if (!int.TryParse(consumptionId, out int Id) || Id <= 0)
            {
                return(new BadRequestObjectResult(Messages.ErrorIncorrectId));
            }
            ConsumptionView consumption = await consumptionRepository.GetConsumptionByConsumptionId(Id);

            int patientId = consumption.PatientId;

            // Auth check
            if (!await Authorised(req, patientId, false))
            {
                return(new BadRequestObjectResult(Messages.AuthNoAcces));
            }

            if (await consumptionRepository.DeleteConsumption(Id, patientId))
            {
                return(new OkObjectResult(Messages.OKDelete));
            }

            return(new BadRequestObjectResult(Messages.ErrorDelete));
        }