Ejemplo n.º 1
0
        public async Task <JsonResult> GetSensorsByUserEmailAndSecurityStamp(CheckModel model)
        {
            var results = new List <ValidationResult>();
            var context = new ValidationContext(model);

            if (!Validator.TryValidateObject(model, context, results, true))
            {
                return(Json("Bad data"));
            }
            ApplicationUserDTO checkUser = new ApplicationUserDTO
            {
                Email         = model.Email,
                SecurityStamp = model.SecurityStamp
            };

            try
            {
                ApplicationUserDTO user = await userService.GetUserByEmailAndSecurityStamp(checkUser);

                if (user.Role != "user")
                {
                    return(Json("Only User can have sensors"));
                }
                else
                {
                    var sensors = sensorService.GetSensorsByUserId(user.Id).ToList();
                    List <GetSensor> getSensors = new List <GetSensor>();
                    foreach (var sensor in sensors)
                    {
                        var        mapper    = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap <SensorDTO, GetSensor>()).CreateMapper();
                        GetSensor  getSensor = mapper.Map <SensorDTO, GetSensor>(sensor);
                        ProductDTO product   = productService.GetProductById(sensor.ProductId);
                        getSensor.ProductName = product.Name;
                        MonitoringDTO      monitoring = monitoringService.GetMonitoringsBySensorId(sensor.Id);
                        ApplicationUserDTO observer   = await userService.GetUserById(monitoring.ApplicationUserId);

                        getSensor.ObserverEmail = observer.Email;
                        getSensors.Add(getSensor);
                    }
                    return(Json(getSensors, JsonRequestBehavior.AllowGet));
                }
            }
            catch
            {
                return(Json("Email or Token is wrong"));
            }
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> GetAllOrdersForAdmin(CheckModel model)
        {
            var results = new List <ValidationResult>();
            var context = new ValidationContext(model);

            if (!Validator.TryValidateObject(model, context, results, true))
            {
                return(Json("Bad data"));
            }

            ApplicationUserDTO checkUser = new ApplicationUserDTO
            {
                Email         = model.Email,
                SecurityStamp = model.SecurityStamp
            };

            try
            {
                ApplicationUserDTO user = await userService.GetUserByEmailAndSecurityStamp(checkUser);

                if (user.Role != "admin")
                {
                    return(Json("Only Admin can get orders"));
                }
                else
                {
                    List <GetEmptyOrder> orders    = new List <GetEmptyOrder>();
                    List <OrderDTO>      ordersDTO = orderService.GetOrdersWithoutCourier().ToList();
                    foreach (var orderDTO in ordersDTO)
                    {
                        SensorDTO sensor;
                        try
                        {
                            sensor = sensorService.GetSensorById(orderDTO.SensorId);
                        }
                        catch
                        {
                            return(Json("Bad with get sensor"));
                        }
                        MonitoringDTO monitoringDTo;
                        try
                        {
                            monitoringDTo = monitoringService.GetMonitoringsBySensorId(sensor.Id);
                        }
                        catch
                        {
                            return(Json("Bad with get monitoring"));
                        }
                        ApplicationUserDTO customerUser;
                        try
                        {
                            customerUser = await userService.GetUserById(monitoringDTo.ApplicationUserId);
                        }
                        catch
                        {
                            return(Json("Bad with get user"));
                        }
                        ProductDTO productDTO;
                        try
                        {
                            productDTO = productService.GetProductById(sensor.ProductId);
                        }
                        catch
                        {
                            return(Json("Bad with get product"));
                        }
                        GetEmptyOrder order = new GetEmptyOrder
                        {
                            CountProduct    = sensor.CountProduct,
                            CustomerEmail   = customerUser.Email,
                            DeliveryAddress = orderDTO.DeliveryAddress,
                            Price           = orderDTO.Price,
                            ProductName     = productDTO.Name,
                            Id = orderDTO.Id
                        };
                        orders.Add(order);
                    }

                    return(Json(orders, JsonRequestBehavior.AllowGet));
                }
            }
            catch
            {
                return(Json("Email or Token is wrong"));
            }
        }