Пример #1
0
        public IActionResult AccessToController([FromBody] ControllerAccessViewModel model)
        {
            if (model == null)
            {
                return(StatusCode(500, new InternalServerError()));
            }

            var user = User.GetUser(_context);

            var userHasController = _context
                                    .UserHasControllers.Where(p => p.UserId == user.Id)
                                    .Where(p => p.ControllerId == model.ControllerId)
                                    .Where(p => p.IsAdmin)
                                    .FirstOrDefault();

            if (userHasController == null)
            {
                return(Unauthorized(new UnauthorizedError()));
            }

            var accessUser = _context
                             .Users
                             .Where(p => p.Email.ToLower(CultureInfo.CurrentCulture) == model.UserName.ToLower(CultureInfo.CurrentCulture) || p.UserName == model.UserName)
                             .FirstOrDefault();

            if (accessUser == null)
            {
                return(NotFound(new NotFoundError("User not found!")));
            }

            var accessUserHasControler = _context
                                         .UserHasControllers.Where(p => p.UserId == accessUser.Id)
                                         .Where(p => p.ControllerId == model.ControllerId)
                                         .FirstOrDefault();

            if (accessUserHasControler != null)
            {
                return(StatusCode(500, new InternalServerError("User has already added!")));
            }

            var accessUserHasController = new UserHasController
            {
                IsAdmin      = false,
                ControllerId = model.ControllerId,
                UserId       = accessUser.Id
            };

            _context.UserHasControllers.Add(accessUserHasController);
            _context.SaveChanges();
            return(Json(accessUserHasController.Adapt <UserHasControllerViewModel>()));
        }
        public IActionResult Put([FromBody] ControllerViewModel model)
        {
            //Перевіряємо модель
            if (model == null || !ModelState.IsValid)
            {
                return(StatusCode(500, new InternalServerError("Data is incorrect!")));
            }
            var controller = _context.Controllers.Where(p => p.MAC == model.MAC).FirstOrDefault();

            if (controller != null)
            {
                return(StatusCode(500, new InternalServerError("Controller is already exists")));
            }
            if (model.MAC.Length != 12)
            {
                return(StatusCode(500, new InternalServerError("MAC address must has 12 symbols!")));
            }
            //Беремо айді користувача з токену
            var user = User.GetUser(_context);

            if (user == null)
            {
                return(Unauthorized(new UnauthorizedError()));
            }
            //Створюємо контролер
            controller = model.Adapt <Data.Models.Controller>();
            controller.InstalledDate = DateTime.Now;
            controller.PublicKey     = "jak to bedzie pracowac, pliz ktos wie?";
            _context.Controllers.Add(controller);
            //Створюємо UserHasController
            var userHasController = new UserHasController();

            userHasController.ControllerId = controller.Id;
            userHasController.UserId       = user.Id;
            userHasController.IsAdmin      = true;
            _context.UserHasControllers.Add(userHasController);
            //Зберігаємо зміни
            _context.SaveChanges();
            //Створюємо віртуальний пристрій
            var device = new Device
            {
                Name         = "Notification",
                Pin          = 0,
                MAC          = null,
                Status       = true,
                ControllerId = controller.Id,
                DeviceTypeId = _context.DeviceTypes.Where(p => p.TypeName == "Virtual").FirstOrDefault().Id,
            };

            _context.Devices.Add(device);
            _context.SaveChanges();
            //Ствоюємо UserasDevice
            var userHasDevice = new UserHasDevice
            {
                DeviceId = device.Id,
                UsersHaveControllerId = userHasController.Id
            };

            _context.UserHasDevices.Add(userHasDevice);
            _context.SaveChanges();
            //Повертаємо створений контролер як резльтат
            return(new JsonResult(controller.Adapt <ControllerViewModel>()));
        }