public BaseResponse <object> Create(int playerId, CompanyRequest request) { var validationResult = Validate <object>(request); if (!validationResult.Ok) { return(validationResult); } var company = new Company { Name = request.Name, Desciprion = request.Desciprion, }; var companyJoinInfo = new CompanyJoinInfo { Company = company, Key = Guid.NewGuid().ToString() //TODO move it to invite generator }; var companyPlayerOwner = new CompanyPlayer { Company = company, IsOwner = true, PlayerId = playerId }; _dbContext.Companies.Add(company); _dbContext.CompanyJoinInfos.Add(companyJoinInfo); _dbContext.CompanyPlayers.Add(companyPlayerOwner); return(BaseResponse <object> .Success()); }
public BaseResponse <object> JoinCompany(int playerId, string companyJoinCode) { if (!_dbContext.Players.Any(x => x.Id == playerId)) { return(BaseResponse <object> .Fail(_errorMessageProvider.PlayerNotFound)); } var company = _dbContext.Companies.FirstOrDefault(x => x.CompanyJoinInfo.Key == companyJoinCode); if (company == null) { return(BaseResponse <object> .Fail(_errorMessageProvider.JoinKeyIsInvalid)); } if (_dbContext.CompanyPlayers.Any(x => x.CompanyId == company.Id && x.PlayerId == playerId)) { return(BaseResponse <object> .Fail(_errorMessageProvider.PlayerAlreadyAddedToCompany)); } var companyPlayer = new CompanyPlayer { IsOwner = false, CompanyId = company.Id, PlayerId = playerId, }; _dbContext.CompanyPlayers.Add(companyPlayer); _dbContext.SaveChanges(); return(BaseResponse <object> .Success()); }