コード例 #1
0
        public BuildSessionModel InitBuildSession(StartBuildDto startBuildDto)
        {
            SessionService.CheckSession(startBuildDto.Session);
            FurnitureItemModel furniture = FurnitureRepo.Get(startBuildDto.FurnitureId.Value);

            if (furniture == null)
            {
                throw new NotFoundException("furniture");
            }

            bool canBuild = FurnitureService.CanBuild(startBuildDto.Session, startBuildDto.FurnitureId.Value);

            if (!canBuild)
            {
                throw new NotFoundException("all owned parts");
            }

            int userId = startBuildDto.Session.UserId.Value;

            if (UserBuildTokens.ContainsKey(userId))
            {
                RemoveSessionByUserId(userId);
            }
            //throw new ConflictException("build sessions");

            string token = HashingService.GetHash(Guid.NewGuid().ToString());

            IEnumerable <ConcretePartModel> possiblePartsToUse = ConcretePartRepo
                                                                 .GetOwnedByUser(userId)
                                                                 .Where(part => !part.IsForSell && !part.IsInUse)
                                                                 .ToList();

            BuildSessionManager buildSessionInfo = new BuildSessionManager(
                userId, possiblePartsToUse, furniture,
                new BuildSessionModel(token, furniture.Id)
                );

            UserBuildTokens.Add(userId, token);
            BuildSessions.Add(token, buildSessionInfo);
            return(buildSessionInfo.BuildSession);
        }