public ActionResult AddCategory(AddCategoryViewModel vm)
        {
            var Success = false;
            var Message = "";

            var nameValidation = AssetModelCategory.Validator.ValidateName(null, vm.Name);

            if (nameValidation != null)
            {
                ModelState.AddModelError("Name", nameValidation);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var category = new AssetModelCategory {
                        Name = vm.Name
                    };
                    db.AssetModelCategories.Add(category);
                    db.SaveChanges();
                    Success = true;
                    Message = AssetModelCategory.SAVE_SUCCESS;
                }
                catch (Exception)
                {
                    Message = AssetModelCategory.SAVE_FAIL;
                }
                return(Json(new { Success, Message }));
            }
            return(PartialView("_AddCategory", vm));
        }
Beispiel #2
0
            public void Execute(IJobExecutionContext context)
            {
                try
                {
                    using (var db = new AssetHubContext())
                    {
                        var x = (from a in db.Assets
                                 join l in db.Loans on a.Id equals l.AssetId
                                 where DateTime.Now >= l.TimeFrom && DateTime.Now <= l.TimeTo &&
                                 l.RoomId != a.Locations.Where(al => al.TimeTo == null).Select(al => al.RoomId).FirstOrDefault()

                                 select new
                        {
                            AssetId = a.Id,
                            RoomId = l.RoomId
                        }).ToList();

                        foreach (var y in x)
                        {
                            var currentLocation = (from l in db.AssetLocations
                                                   where l.TimeTo == null && l.AssetId == y.AssetId
                                                   select l).FirstOrDefault();
                            if (currentLocation != null)
                            {
                                currentLocation.TimeTo = DateTime.Now;
                            }

                            db.AssetLocations.Add(new AssetLocation
                            {
                                AssetId  = y.AssetId,
                                RoomId   = y.RoomId,
                                TimeFrom = DateTime.Now,
                                TimeTo   = null,
                            });
                        }

                        db.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ;
                }
            }
Beispiel #3
0
        public ActionResult RoomManagement(RoomManagementViewModel vm)
        {
            var Success = false;
            var Message = "";

            if (vm.Rooms != null)
            {
                for (var i = 0; i < vm.Rooms.Count; i++)
                {
                    var roomValidation = Room.Validator.ValidateName(vm.Rooms[i].Name);
                    if (roomValidation != null)
                    {
                        ModelState.AddModelError($"Rooms[{i}].Name", roomValidation);
                    }
                }
            }

            if (vm.NewRooms != null)
            {
                for (var i = 0; i < vm.NewRooms.Count; i++)
                {
                    var roomValidation = Room.Validator.ValidateName(vm.NewRooms[i].Name);
                    if (roomValidation != null)
                    {
                        ModelState.AddModelError($"NewRooms[{i}].Name", roomValidation);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (vm.DeletedRooms != null)
                    {
                        foreach (var r in vm.DeletedRooms)
                        {
                            db.Entry(new Room {
                                Id = r.Id
                            }).State = EntityState.Deleted;
                        }
                    }

                    if (vm.Rooms != null)
                    {
                        foreach (var r in vm.Rooms)
                        {
                            var room = db.Rooms.Find(r.Id);
                            room.Name = r.Name;
                        }
                    }

                    if (vm.NewRooms != null)
                    {
                        foreach (var r in vm.NewRooms)
                        {
                            db.Rooms.Add(new Room
                            {
                                Name = r.Name,
                            });
                        }
                    }

                    db.SaveChanges();

                    Success = true;
                    Message = Room.SAVE_SUCCESS;
                }
                catch (Exception e)
                {
                    Message = Room.SAVE_FAIL;
                }
                return(Json(new { Success, Message }));
            }
            return(PartialView("_RoomManagement", vm));
        }
Beispiel #4
0
        public ActionResult AddAsset(AddAssetViewModel vm)
        {
            var Success = false;
            var Message = "";

            var nameValidation = Asset.Validator.ValidateName(vm.Name);

            if (nameValidation != null)
            {
                ModelState.AddModelError("Name", nameValidation);
            }

            var valueValidation = Asset.Validator.ValidateValue(vm.Value);

            if (valueValidation != null)
            {
                ModelState.AddModelError("Value", valueValidation);
            }

            var modelValidation = Asset.Validator.ValidateAssetModel(vm.SelectedAssetModelId);

            if (modelValidation != null)
            {
                ModelState.AddModelError("AssetModel", modelValidation);
            }

            var serialValidation = Asset.Validator.ValidateSerialKey(vm.SerialNumber);

            if (serialValidation != null)
            {
                ModelState.AddModelError("SerialNumber", serialValidation);
            }

            if (vm.Properties != null)
            {
                for (var i = 0; i < vm.Properties.Count; i++)
                {
                    var p                  = vm.Properties[i];
                    var property           = db.AssetModelProperties.Find(p.PropertyId);
                    var propertyValidation = Asset.Validator.ValidatePropertyValue(property, p.Value);
                    if (propertyValidation != null)
                    {
                        ModelState.AddModelError($"Properties[{i}]", propertyValidation);
                    }
                }
            }

            if (vm.SelectedRoomId < 0)
            {
                ModelState.AddModelError("SelectedRoomId", "Room is required");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var asset = new Asset
                    {
                        AssetModelId    = vm.SelectedAssetModelId,
                        Name            = vm.Name,
                        Value           = vm.Value,
                        SerialNumber    = vm.SerialNumber,
                        AssetProperties = new List <AssetProperty>(),
                        Locations       = new List <AssetLocation>(),
                    };

                    if (vm.Properties != null)
                    {
                        foreach (var p in vm.Properties)
                        {
                            asset.AssetProperties.Add(new AssetProperty
                            {
                                Asset = asset,
                                AssetModelPropertyId = p.PropertyId,
                                Value = p.Value,
                            });
                        }
                    }

                    var room     = db.Rooms.Find(vm.SelectedRoomId);
                    var location = new AssetLocation
                    {
                        Asset    = asset,
                        Room     = room,
                        TimeFrom = DateTime.Now,
                        TimeTo   = null,
                    };

                    db.Assets.Add(asset);
                    db.AssetLocations.Add(location);
                    db.SaveChanges();

                    Success = true;
                    Message = Asset.SAVE_SUCCESS;
                }
                catch (Exception e)
                {
                    Message = Asset.SAVE_FAIL;
                }
                return(Json(new { Success, Message }));
            }

            vm.AssetModels = db.AssetModelDropdown();
            vm.Rooms       = db.RoomDropdown();
            return(PartialView("_AddAsset", vm));
        }
Beispiel #5
0
        public ActionResult EditUser(EditUserViewModel vm)
        {
            var Success = false;
            var Message = "";

            var firstNameValidation = Models.User.Validator.ValidateFirstName(vm.FirstName);

            if (firstNameValidation != null)
            {
                ModelState.AddModelError("FirstName", firstNameValidation);
            }

            var lastNameValidation = Models.User.Validator.ValidateLastName(vm.LastName);

            if (lastNameValidation != null)
            {
                ModelState.AddModelError("LastName", lastNameValidation);
            }

            var emailValidation = Models.User.Validator.ValidateEmail(vm.Id, vm.Email);

            if (emailValidation != null)
            {
                ModelState.AddModelError("Email", emailValidation);
            }

            var positionValidation = Models.User.Validator.ValidatePosition(vm.SelectedPositionId);

            if (positionValidation != null)
            {
                ModelState.AddModelError("SelectedPositionId", positionValidation);
            }

            var roomValidation = Models.User.Validator.ValidateRoom(vm.SelectedRoomId);

            if (roomValidation != null)
            {
                ModelState.AddModelError("SelectedRoomId", roomValidation);
            }

            if (ModelState.IsValid)
            {
                var user = db.Users.Find(vm.Id);

                user.FirstName      = vm.FirstName;
                user.LastName       = vm.LastName;
                user.Email          = user.UserName = vm.Email;
                user.IsAdmin        = vm.IsAdmin;
                user.RoomId         = vm.SelectedRoomId;
                user.UserPositionId = vm.SelectedPositionId;

                try
                {
                    db.SaveChanges();
                    Success = true;
                    Message = Models.User.SAVE_SUCCESS;
                }
                catch (Exception)
                {
                    Message = Models.User.SAVE_FAIL;
                }

                return(Json(new { Success, Message }));
            }

            vm.Rooms     = db.RoomDropdown();
            vm.Positions = db.UserPositionDropdown();
            return(PartialView("_EditUser", vm));
        }
        public ActionResult AddAssetModel(AddAssetModelViewModel vm)
        {
            var Success = false;
            var Message = "";

            var categoryValidation = AssetModel.Validator.ValidateCategory(vm.SelectedCategoryId);

            if (categoryValidation != null)
            {
                ModelState.AddModelError("Category", categoryValidation);
            }

            var nameValidation = AssetModel.Validator.ValidateName(null, vm.Name, vm.SelectedCategoryId);

            if (nameValidation != null)
            {
                ModelState.AddModelError("Name", nameValidation);
            }

            if (vm.Properties != null)
            {
                var hasPropertyErrors = false;
                for (var i = 0; i < vm.Properties.Count; i++)
                {
                    var propertyValidation = AssetModel.Validator.ValidateProperty(vm.Properties[i].Name);
                    if (propertyValidation != null)
                    {
                        ModelState.AddModelError($"Properties[{i}].Name", propertyValidation);
                        hasPropertyErrors = true;
                    }
                }

                if (!hasPropertyErrors)
                {
                    var properties = (from p in vm.Properties
                                      select new Tuple <string, bool>(p.Name, p.IsNumeric)).ToList();
                    var propertiesValidation = AssetModel.Validator.ValidateProperties(properties);
                    if (propertiesValidation != null)
                    {
                        ModelState.AddModelError("Properties", propertiesValidation);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var newModel = new AssetModel
                    {
                        AssetModelCategoryId = vm.SelectedCategoryId,
                        Name       = vm.Name,
                        Properties = new List <AssetModelProperty>(),
                    };
                    if (vm.Properties != null)
                    {
                        foreach (var p in vm.Properties)
                        {
                            newModel.Properties.Add(new AssetModelProperty
                            {
                                AssetModel = newModel,
                                Name       = p.Name,
                                IsNumeric  = p.IsNumeric,
                            });
                        }
                    }
                    db.AssetModels.Add(newModel);
                    db.SaveChanges();

                    Success = true;
                    Message = AssetModel.SAVE_SUCCESS;
                }
                catch (Exception e)
                {
                    Message = AssetModel.SAVE_FAIL;
                }
                return(Json(new { Success, Message }));
            }
            vm.Categories = db.CategoryDropdown();
            return(PartialView("_AddAssetModel", vm));
        }
Beispiel #7
0
        public ActionResult CreateLoan(CreateLoanViewModel vm)
        {
            if (vm.SelectedHourFromId < 0)
            {
                ModelState.AddModelError("TimeFrom", Loan.Validator.TIME_FROM_REQUIRED);
            }
            if (vm.SelectedHourToId < 0)
            {
                ModelState.AddModelError("TimeTo", Loan.Validator.TIME_TO_REQUIRED);
            }

            var timeFrom = vm.DateFrom.AddHours(vm.SelectedHourFromId);
            var timeTo   = vm.DateTo.AddHours(vm.SelectedHourToId);

            var timeFromValidation = Loan.Validator.ValidateTimeTo(timeFrom);

            if (timeFromValidation != null)
            {
                ModelState.AddModelError("TimeFrom", timeFromValidation);
            }

            var timeToValidation = Loan.Validator.ValidateTimeTo(timeTo);

            if (timeToValidation != null)
            {
                ModelState.AddModelError("TimeTo", timeFromValidation);
            }

            var intervalValidation = Loan.Validator.ValidateTimeInterval(timeFrom, timeTo);

            if (intervalValidation != null)
            {
                ModelState.AddModelError("TimeFrom", intervalValidation);
                ModelState.AddModelError("TimeTo", intervalValidation);
            }

            var availableValidation = Loan.Validator.ValidateAssetAvailabilty(vm.AssetId, timeFrom, timeTo);

            if (availableValidation != null)
            {
                ModelState.AddModelError("", availableValidation);
            }

            var roomValidation = Loan.Validator.ValidateRoom(vm.SelectedRoomId);

            if (roomValidation != null)
            {
                ModelState.AddModelError("SelectedRoomId", roomValidation);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var loan = new Loan
                    {
                        AssetId  = vm.AssetId,
                        RoomId   = vm.SelectedRoomId,
                        TimeFrom = timeFrom,
                        TimeTo   = timeTo,
                        UserId   = User.Identity.GetUserId(),
                    };
                    db.Loans.Add(loan);
                    db.SaveChanges();

                    return(Json(new { Success = true, Message = Loan.SAVE_SUCCESS }));
                }
                catch (Exception e)
                {
                    return(Json(new { Success = false, Message = Loan.SAVE_FAIL }));
                }
            }

            vm.Hours = GenerateHours();
            vm.Rooms = db.RoomDropdown();
            return(PartialView("_CreateLoan", vm));
        }