public async Task <ProjectAccommodationType> SaveRoomTypeAsync(ProjectAccommodationType roomType) { if (roomType.ProjectId == 0) { throw new ActivationException("Inconsistent state. ProjectId can't be 0"); } ProjectAccommodationType result; if (roomType.Id != 0) { result = await UnitOfWork.GetDbSet <ProjectAccommodationType>().FindAsync(roomType.Id).ConfigureAwait(false); if (result?.ProjectId != roomType.ProjectId) { return(null); } result.Name = roomType.Name; result.Cost = roomType.Cost; result.Capacity = roomType.Capacity; result.Description = roomType.Description; result.IsAutoFilledAccommodation = roomType.IsAutoFilledAccommodation; result.IsInfinite = roomType.IsInfinite; result.IsPlayerSelectable = roomType.IsPlayerSelectable; } else { result = UnitOfWork.GetDbSet <ProjectAccommodationType>().Add(roomType); } await UnitOfWork.SaveChangesAsync().ConfigureAwait(false); return(result); }
public async Task <IEnumerable <ProjectAccommodation> > AddRooms(int projectId, int roomTypeId, string rooms) { //TODO: Implement rooms names checking ProjectAccommodationType roomType = UnitOfWork.GetDbSet <ProjectAccommodationType>().Find(roomTypeId); if (roomType == null) { throw new JoinRpgEntityNotFoundException(roomTypeId, typeof(ProjectAccommodationType).Name); } if (roomType.ProjectId != projectId) { throw new ArgumentException($"Room type {roomTypeId} is from another project than specified", nameof(roomTypeId)); } // Internal function // Creates new room using name and parameters from given room info ProjectAccommodation CreateRoom(string name) => new ProjectAccommodation { Name = name, AccommodationTypeId = roomTypeId, ProjectId = projectId, ProjectAccommodationType = roomType, }; // Internal function // Iterates through rooms list and creates object for each room from a list IEnumerable <ProjectAccommodation> CreateRooms(string r) { foreach (string roomCandidate in r.Split(',')) { int rangePos = roomCandidate.IndexOf('-'); if (rangePos > -1) { if (int.TryParse(roomCandidate.Substring(0, rangePos).Trim(), out int roomsRangeStart) && int.TryParse(roomCandidate.Substring(rangePos + 1).Trim(), out int roomsRangeEnd) && roomsRangeStart < roomsRangeEnd) { while (roomsRangeStart <= roomsRangeEnd) { yield return(CreateRoom(roomsRangeStart.ToString())); roomsRangeStart++; } // Range was defined correctly, we can continue to next item continue; } } yield return(CreateRoom(roomCandidate.Trim())); } } IEnumerable <ProjectAccommodation> result = UnitOfWork.GetDbSet <ProjectAccommodation>().AddRange(CreateRooms(rooms)); await UnitOfWork.SaveChangesAsync(); return(result); }
public RoomTypeViewModel([NotNull] ProjectAccommodationType entity, int userId) : this(entity.Project, userId) { if (entity.ProjectId == 0 || entity.Id == 0) { throw new ArgumentException("Entity must be valid object"); } Id = entity.Id; Cost = entity.Cost; Name = entity.Name; Capacity = entity.Capacity; IsInfinite = entity.IsInfinite; IsPlayerSelectable = entity.IsPlayerSelectable; IsAutoFilledAccommodation = entity.IsAutoFilledAccommodation; DescriptionEditable = entity.Description.Contents; DescriptionView = entity.Description.ToHtmlString(); // Creating a list of requests associated with this room type Requests = entity.Desirous.Select(ar => new AccRequestViewModel(ar)).ToList(); // Creating a list of requests not assigned to any room List <AccRequestViewModel> ua = Requests.Where(ar => ar.RoomId == 0).ToList(); ua.Sort((x, y) => { int result = x.Persons - y.Persons; if (result == 0) { result = x.FeeToPay - y.FeeToPay; } if (result == 0) { result = string.Compare(x.PersonsList, y.PersonsList, StringComparison.CurrentCultureIgnoreCase); } return(result); }); UnassignedRequests = ua; // Creating a list of rooms contained in this room type List <RoomViewModel> rl = entity.ProjectAccommodations.Select(acc => new RoomViewModel(acc, this)).ToList(); rl.Sort((x, y) => { if (x.Occupancy == y.Occupancy) { if (int.TryParse(x.Name, out int xn) && int.TryParse(y.Name, out int yn)) { return(xn - yn); } return(string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase)); } if (x.Occupancy == x.Capacity) { return(1); } if (y.Occupancy == y.Capacity) { return(-1); } return(y.Occupancy - x.Occupancy); }); Rooms = rl; }