Exemplo n.º 1
0
        public async Task <EquipmentType> Create(string name, string description, int amount)
        {
            var existingEquipmentType = await GetByName(name);

            if (existingEquipmentType != null)
            {
                throw new EquipmentTypeAlreadyExistsException(name);
            }

            EquipmentType equipmentType = new EquipmentType()
            {
                Name        = name,
                Description = description,
                IsActive    = true
            };

            var createdEquipmentType = await Create(equipmentType);

            // Fire and forget
            await Task.Factory.StartNew(() => _equipmentItemService.Create(createdEquipmentType, amount));

            return(createdEquipmentType);
        }
        private async void ExecuteScheduleRenovation()
        {
            RoomAlreadyInUse = InvalidTimeFrame = false;
            var renovationStartPeriod = RenovationStartDate + RenovationStartTime?.TimeOfDay;
            var renovationEndPeriod   = RenovationEndDate + RenovationEndTime?.TimeOfDay;

            if (renovationEndPeriod <= renovationStartPeriod || renovationStartPeriod == null || renovationEndPeriod == null)
            {
                InvalidTimeFrame = true;
                return;
            }

            // Trying to save some RAM
            if ((await _calendarEntryService.GetAllByRoomAndTimeFrame(CurrentlySelectedRoom.Room, renovationStartPeriod.Value, renovationEndPeriod.Value)).Count != 0)
            {
                RoomAlreadyInUse = true;
                return;
            }

            var addedItems = await AddedEquipmentItems();

            var removedItems = await RemovedEquipmentItems();

            var roomToJoinTo = RoomToJoinTo?.Room;

            await _renovationService.Create(CurrentlySelectedRoom.Room, roomToJoinTo, NewRoomType,
                                            renovationStartPeriod.Value, renovationEndPeriod.Value, SplittingRoom,
                                            removedItems, addedItems);

            if (removedItems.Count > 0)
            {
                foreach (var equipmentItem in removedItems)
                {
                    await _equipmentItemService.Create(equipmentItem.EquipmentType, 1);
                }
            }

            Calendar.AddEvents((await _calendarEntryService.GetAllByRoomAndTimeFrame(CurrentlySelectedRoom.Room, renovationStartPeriod.Value, renovationEndPeriod.Value)).ToList());
            RenovationEndDate = RenovationStartDate = RenovationStartTime = RenovationEndTime = null;
            SplittingRoom     = false;
            OtherRenovations  = false;
            AddedEquipmentTypes.Clear();
            RemovedEquipmentTypes.Clear();
            LoadFreeEquipmentTypes();
            LoadRoomEquipmentTypes();
        }
Exemplo n.º 3
0
        public async Task <bool> Execute(Renovation renovation)
        {
            if (renovation.Completed || renovation.EndDateTime > DateTime.Now)
            {
                return(false);
            }

            renovation = await Get(renovation.ID);

            var roomToUpdate = await _roomService.Get(renovation.Room.ID);

            roomToUpdate.Type = renovation.NewRoomType;

            if (renovation.AddedEquipmentItems.Count > 0)
            {
                var itemsInRoom = roomToUpdate.Equipment.ToList();
                roomToUpdate.Equipment = new List <EquipmentItem>();
                await _roomService.Update(roomToUpdate, room => room.Equipment);

                foreach (var equipmentItem in renovation.AddedEquipmentItems.ToList())
                {
                    roomToUpdate.Equipment.Add(await _equipmentItemService.Get(equipmentItem.ID));
                }

                itemsInRoom.ForEach(ei => roomToUpdate.Equipment.Add(ei));

                await _roomService.Update(roomToUpdate, room => room.Equipment);

                foreach (var equipmentItem in renovation.AddedEquipmentItems)
                {
                    await _equipmentItemService.Create(equipmentItem.EquipmentType, 1);
                }
            }

            if (renovation.RemovedEquipmentItems.Count > 0)
            {
                var itemsInRoom = roomToUpdate.Equipment.ToList();
                roomToUpdate.Equipment = new List <EquipmentItem>();
                await _roomService.Update(roomToUpdate, room => room.Equipment);

                var idsToRemove = new HashSet <Guid>(renovation.RemovedEquipmentItems.Select(x => x.ID));

                foreach (var equipmentItem in itemsInRoom)
                {
                    roomToUpdate.Equipment.Add(await _equipmentItemService.Get(equipmentItem.ID));
                }

                roomToUpdate.Equipment = roomToUpdate.Equipment
                                         .Where(equipmentItem => !idsToRemove.Contains(equipmentItem.ID))
                                         .ToList();
                await _roomService.Update(roomToUpdate, room => room.Equipment);
            }

            if (renovation.Splitting)
            {
                var newRoom = new Room
                {
                    Floor    = renovation.Room.Floor,
                    Type     = renovation.NewRoomType,
                    IsActive = true,
                };

                if (!char.IsLetter(renovation.Room.Number.Last()))
                {
                    newRoom.Number       = renovation.Room.Number + "B";
                    roomToUpdate.Number += "A";
                }
                else
                {
                    newRoom.Number       = renovation.Room.Number + "2";
                    roomToUpdate.Number += "1";
                }
                await _roomService.Create(newRoom);

                await _roomService.Update(roomToUpdate);
            }

            else if (renovation.RoomToAdd != null)
            {
                renovation.RoomToAdd.IsActive = false;
                await _roomService.Update(renovation.RoomToAdd);
            }

            renovation = await Get(renovation.ID);

            renovation.Completed = true;
            await Update(renovation);

            return(true);
        }