Ejemplo n.º 1
0
        public async Task <IActionResult> AddAlert([FromBody] AlertData alData)
        {
            User user = await _auth.GetUser(this.User.Identity.Name);

            Permissions permissions = await _auth.GetPermissions(user.Id);

            if (permissions.AddAlert == false)
            {
                return(Unauthorized());
            }

            if (ModelState.IsValid)
            {
                //check if new or updating alert

                Alert alert = _repo.GetAlertByInvId(alData.AlertInvId).Result;

                if (alert == null)
                {
                    alert.Threshold = alData.Threshold;
                    //alert.DateUnder = alData.DateUnder;

                    var updatedAlert = await _repo.UpdateAlert(alert);
                }
                else //create a new alert
                {
                    var alertToCreate = new Alert
                    {
                        Threshold = alData.Threshold,
                        //DateUnder = alData.DateUnder,
                        DateUnder = DateTime.MinValue,
                        //DateOrdered = alData.DateOrdered,
                        DateOrdered = DateTime.MinValue,
                        //AlertOn = alData.AlertOn,
                        AlertOn = true,
                        //AlertTriggered = alData.AlertTriggered,
                        AlertTriggered = false,
                        AlertInvId     = alData.AlertInvId
                    };

                    //Create new alert
                    var createdAlert = await _repo.AddAlert(alertToCreate);

                    //Add Alert to Inv and Inv to Alert
                    alertToCreate.AlertInv = await _inv.SetAlert(alertToCreate);

                    //Update Alert
                    var updatedAlert = await _repo.UpdateAlert(alertToCreate);
                }

                //created at root status code
                return(StatusCode(201));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }