Beispiel #1
0
 def load(self, club_id):
     try:
         record = self._store[club_id]
     except KeyError:
         raise exception.NotFoundException(entities.Club, club_id)
     else:
         return entities.Club.from_data(record)
Beispiel #2
0
    def delete(self, club):
        obj = self.__session.query(model.Club).get(club.id)
        if obj is None:
            raise exception.NotFoundException(entities.Club, club.id)

        self.__session.delete(obj)
        self.__session.flush()
Beispiel #3
0
 def load(self, id: int) -> entities.Competition:
     try:
         record = self._store[id]
     except KeyError:
         raise exception.NotFoundException(entities.Competition, id)
     else:
         return entities.Competition.from_data(record)
Beispiel #4
0
    def load(self, competitor_id: int) -> entities.Competitor:
        record = self.__session.query(model.Competitor).get(competitor_id)
        if record is None:
            raise exception.NotFoundException(entities.Competitor,
                                              competitor_id)

        return entities.Competitor.from_data(mapper_as_dict(record))
Beispiel #5
0
    def delete(self, match: entities.Match) -> None:
        obj = self.__session.query(model.Match).get(match.id)
        if obj is None:
            raise exception.NotFoundException(entities.Match, match.id)

        self.__session.delete(obj)
        self.__session.flush()
Beispiel #6
0
 def find_by_name(self, name):
     user = self.__session.query(
         model.User).filter(model.User.name == name).one_or_none()
     if user is None:
         raise exception.NotFoundException(self, name)
     else:
         return entities.User.from_data(user.__dict__)
Beispiel #7
0
 def load(self, id_):
     try:
         record = self._store[id_]
     except KeyError:
         raise exception.NotFoundException(entities.Match, id_)
     else:
         return entities.Match.from_data(record)
Beispiel #8
0
    def delete(self, competition: entities.Competition):
        record = self.__session.query(model.TournamentCompetition).get(
            competition.id)
        if record is None:
            raise exception.NotFoundException(entities.Competition,
                                              competition.id)

        self.__session.delete(record)
        self.__session.flush()
Beispiel #9
0
    def load(self, id_):
        try:
            record = self._store[id_]
        except KeyError:
            raise exception.NotFoundException(entities.Phase, id_)
        else:
            phase = entities.Phase.from_data(record)
            phase.matches = self._context.match.find_by_phase(phase.id)

            return phase
Beispiel #10
0
    def save(self, competitor: entities.Competitor) -> entities.Competitor:
        try:
            record = self._store[competitor.id]
        except KeyError:
            raise exception.NotFoundException(entities.Competitor,
                                              competitor.id)
        else:
            record['first_name'] = competitor.first_name
            record['last_name'] = competitor.last_name
            record['birthday'] = competitor.birthday
            record['gender'] = competitor.gender
            record['club_id'] = competitor.club_id

            return entities.Competitor.from_data(record)
Beispiel #11
0
    def delete(self, competitor: entities.Competitor) -> None:
        obj = self.__session.query(model.Competitor).get(competitor.id)
        if obj is None:
            raise exception.NotFoundException(entities.Competitor,
                                              competitor.id)

        try:
            self.__session.delete(obj)
            self.__session.flush()
        except sa.exc.IntegrityError as ex:
            raise exception.ConstraintViolatedException(
                entities.Competitor,
                competitor.id,
                str(ex),
            )
Beispiel #12
0
    def save(self, competitor: entities.Competitor) -> entities.Competitor:
        record = self.__session.query(model.Competitor).get(
            competitor.id)  # type: model.Competitor
        if record is None:
            raise exception.NotFoundException(entities.Competitor,
                                              competitor.id)

        record.first_name = competitor.first_name
        record.last_name = competitor.last_name
        record.birthday = competitor.birthday
        record.gender = competitor.gender
        record.club_id = competitor.club_id

        self.__session.add(record)
        self.__session.flush()

        return entities.Competitor.from_data(mapper_as_dict(record))
Beispiel #13
0
    def save(self, competition) -> entities.Competition:
        record = self.__session.query(model.TournamentCompetition).get(
            competition.id)
        if record is None:
            raise exception.NotFoundException(entities.Competition,
                                              competition.id)

        record.tournament_id = competition.tournament.id
        record.name = competition.name
        record.scheme = competition.scheme
        record.gender = competition.gender
        record.minimum_age = competition.minimum_age
        record.maximum_age = competition.maximum_age
        record.minimum_weight = competition.minimum_weight
        record.maximum_weight = competition.maximum_weight

        old_competitor_ids = {
            competitor.competitor.id
            for competitor in record.competitors
        }
        new_competitor_ids = {
            competitor.id
            for competitor in competition.competitors
        }

        with self.__session.begin_nested():
            for competitor in list(record.competitors):
                if competitor.competitor.id not in new_competitor_ids:
                    record.competitors.remove(competitor)

            for competitor_id in new_competitor_ids - old_competitor_ids:
                record.competitors.append(
                    model.TournamentCompetitionCompetitor(
                        competition_id=record.id,
                        competitor_id=competitor_id,
                    ))

            self.__session.add(record)
            self.__session.flush()

            return entities.Competition.from_data(mapper_as_dict(record))
Beispiel #14
0
    def save(self, match: entities.Match) -> entities.Match:
        obj = self.__session.query(model.Match).get(match.id)
        if obj is None:
            raise exception.NotFoundException(entities.Match, match.id)
        
        obj.round = match.round
        obj.position = match.position
        obj.pool = match.pool

        new_ids = {c.id for c in match.competitors}
        to_be_deleted = [
            competitor
            for competitor in obj.competitors
            if competitor.competitor_id not in new_ids
        ]

        for competitor in to_be_deleted:
            obj.competitors.remove(competitor)

        for position, competitor in enumerate(match.competitors):
            candidate = self.__session.query(model.MatchCompetitor).filter(
                sa.and_(
                    model.MatchCompetitor.match_id == obj.id,
                    model.MatchCompetitor.competitor_id == competitor.id,
                ),
            ).one_or_none()
            if candidate is None:
                candidate = model.MatchCompetitor(
                    match_id=match.id,
                    competitor_id=competitor.id,
                )
                obj.competitors.append(candidate)

            candidate.position = position
            candidate.result = match._result.get(competitor.id)

        self.__session.add(obj)
        self.__session.flush()

        return entities.Match.from_data(mapper_as_dict(obj))
Beispiel #15
0
    def save(self, tournament: entities.Tournament) -> entities.Tournament:
        if tournament.id not in self._store:
            raise exception.NotFoundException(entities.Tournament, tournament.id)

        self._store[tournament.id] = attr.asdict(tournament)
        return entities.Tournament.from_data(self._store[tournament.id])
Beispiel #16
0
 def load(self, id: int) -> entities.Tournament:
     try:
         return entities.Tournament.from_data(self._store[id])
     except KeyError:
         raise exception.NotFoundException(entities.Tournament, id)
Beispiel #17
0
    def delete(self, competitor: entities.Competitor) -> None:
        if competitor.id not in self._store:
            raise exception.NotFoundException(entities.Competitor,
                                              competitor.id)

        del self._store[competitor.id]
Beispiel #18
0
 def load(self, competitor_id: int) -> entities.Competitor:
     try:
         return entities.Competitor.from_data(self._store[competitor_id])
     except KeyError:
         raise exception.NotFoundException(entities.Competitor,
                                           competitor_id)
Beispiel #19
0
 def load(self, id_) -> entities.Match:
     obj = self.__session.query(model.Match).get(id_)
     if obj is None:
         raise exception.NotFoundException(entities.Match, id_)
     else:
         return entities.Match.from_data(mapper_as_dict(obj))
Beispiel #20
0
    def delete(self, competition: entities.Competition):
        if competition.id not in self._store:
            raise exception.NotFoundException(entities.Competition,
                                              competition.id)

        del self._store[competition.id]
Beispiel #21
0
 def load(self, id) -> entities.Competition:
     record = self.__session.query(model.TournamentCompetition).get(id)
     if record is None:
         raise exception.NotFoundException(entities.Competition, id)
     else:
         return entities.Competition.from_data(mapper_as_dict(record))
Beispiel #22
0
    def find_by_name(self, name):
        for record in self._store.values():
            if record['name'] == name:
                return entities.User.from_data(record)

        raise exception.NotFoundException(entities.User, name)
Beispiel #23
0
 def load(self, club_id):
     obj = self.__session.query(model.Club).get(club_id)
     if obj is None:
         raise exception.NotFoundException(self, club_id)
     else:
         return entities.Club(id=obj.id, name=obj.name)
Beispiel #24
0
 def delete(self, club):
     try:
         del self._store[club.id]
     except KeyError:
         raise exception.NotFoundException(entities.Club, club.id)
Beispiel #25
0
    def delete(self, match: entities.Match) -> None:
        if match.id not in self._store:
            raise exception.NotFoundException(entities.Match, match.id)

        del self._store[match.id]