Пример #1
0
    protected async Task <GenericResultRecord <LeagueYearPublisherRecord> > GetExistingLeagueYearAndPublisher(Guid publisherID,
                                                                                                              ActionProcessingModeBehavior actionProcessingModeBehavior, RequiredRelationship requiredRelationship, RequiredYearStatus requiredYearStatus)
    {
        LeagueYearKey?leagueYearKey = await _fantasyCriticService.GetLeagueYearKeyForPublisherID(publisherID);

        if (leagueYearKey is null)
        {
            return(GetFailedResult <LeagueYearPublisherRecord>(BadRequest("Publisher does not exist.")));
        }

        var leagueYearRecord = await GetExistingLeagueYear(leagueYearKey.LeagueID, leagueYearKey.Year, actionProcessingModeBehavior, requiredRelationship, requiredYearStatus);

        if (leagueYearRecord.FailedResult is not null)
        {
            return(GetFailedResult <LeagueYearPublisherRecord>(leagueYearRecord.FailedResult));
        }

        var publisher = leagueYearRecord.ValidResult !.LeagueYear.GetPublisherByID(publisherID);

        if (publisher is null)
        {
            return(GetFailedResult <LeagueYearPublisherRecord>(BadRequest("Publisher does not exist in that league.")));
        }

        bool userIsPublisher = leagueYearRecord.ValidResult.CurrentUser is not null &&
                               leagueYearRecord.ValidResult.CurrentUser.Id == publisher.User.Id;

        if (requiredRelationship.MustBePublisher && !userIsPublisher)
        {
            return(GetFailedResult <LeagueYearPublisherRecord>(Forbid()));
        }

        var publisherRelationship = new PublisherUserRelationship(leagueYearRecord.ValidResult.Relationship, userIsPublisher);

        return(new GenericResultRecord <LeagueYearPublisherRecord>(new LeagueYearPublisherRecord(leagueYearRecord.ValidResult.CurrentUser, leagueYearRecord.ValidResult.LeagueYear, publisher, publisherRelationship), null));
    }