예제 #1
0
        //Save Crew
        public CrewEdit SaveCrew(CrewEdit model)
        {
            if (model != null && Guid.TryParse(model.JobId, out Guid jobid))
            {
                using (var context = new CloudbassContext())
                {
                    var crew = new Models.Crew()
                    {
                        JobId      = jobid,
                        has_RoleId = model.has_RoleId,

                        start_date = model.start_date,

                        end_date  = model.end_date,
                        totalDays = model.totalDays,
                    };


                    context.Crews.Add(crew);
                    context.SaveChanges();


                    return(model);
                }
            }

            // Return false if customeredit == null or CustomerID is not a guid
            return(null);
        }
        public async Task <IActionResult> GetByCrewAndPosition(int id, int position)
        {
            Models.Crew crew = await _context.Crews.Include("Athletes.Athlete").FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            if ((crew.BoatClass == Models.BoatClass.Eight && position > 9) ||
                (crew.BoatClass == Models.BoatClass.CoxedFour && position > 5) ||
                ((crew.BoatClass == Models.BoatClass.CoxlessFour || crew.BoatClass == Models.BoatClass.QuadScull) && position > 4) ||
                (crew.BoatClass == Models.BoatClass.CoxedPair && position > 3) ||
                ((crew.BoatClass == Models.BoatClass.CoxlessPair || crew.BoatClass == Models.BoatClass.DoubleScull) && position > 2) ||
                (crew.BoatClass == Models.BoatClass.SingleScull && position > 1))
            {
                return(BadRequest());
            }

            Models.CrewAthlete crewAthlete = crew.Athletes.FirstOrDefault(x => x.Position == position);

            if (crewAthlete == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <Athlete>(crewAthlete)));
        }
예제 #3
0
        public async Task <IActionResult> GetById(int id)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Results).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            Models.Competition comp = await _context.Competitions.Include(c => c.TimingPoints)
                                      .FirstOrDefaultAsync(c => c.CompetitionId == crew.CompetitionId);

            Crew output = ResultsHelper.BuildCrew(_mapper, comp, crew);

            if (comp.TimingPoints.Count > 0)
            {
                Models.TimingPoint lastPoint = comp.TimingPoints.Last();
                List <Models.Crew> crews     = ResultsHelper.OrderCrews(comp.Crews, comp, lastPoint);
                output.Rank = crew.Rank(crews, comp.TimingPoints.First(), lastPoint);
            }
            for (int i = 1; i < crew.Results.Count; i++)
            {
                List <Models.Crew> crews = ResultsHelper.OrderCrews(comp.Crews, comp, comp.TimingPoints[i]);
                output.Results[i].Rank = crew.Rank(crews, comp.TimingPoints.First(), comp.TimingPoints[i]);
            }
            return(Ok(output));
        }
예제 #4
0
        private async Task <List <Result> > GetResults(Models.Crew crew)
        {
            List <Models.Crew> allCrews = await _context.Crews.Include(c => c.Results)
                                          .Include("Results.TimingPoint").ToListAsync();

            Models.Result previousResult = null;
            bool          isFirst        = true;

            Models.TimingPoint startPoint = crew.Competition.TimingPoints.First();

            List <Result> results = new List <Result>();

            foreach (Models.Result result in crew.Results.OrderBy(x => x.TimingPoint.Order))
            {
                results.Add(new Result()
                {
                    Id          = result.TimingPointId,
                    Name        = result.TimingPoint.Name,
                    TimeOfDay   = result.TimeOfDay,
                    SectionTime = isFirst ? null : crew.RunTime(previousResult.TimingPoint, result.TimingPoint),
                    RunTime     = isFirst ? null : crew.RunTime(startPoint, result.TimingPoint),
                    Rank        = isFirst ? String.Empty : crew.Rank(allCrews.Where(x => x.RunTime(startPoint, result.TimingPoint).HasValue)
                                                                     .OrderBy(x => x.RunTime(startPoint, result.TimingPoint)).ToList(),
                                                                     startPoint, result.TimingPoint)
                });
                previousResult = result;
                isFirst        = false;
            }
            return(results);
        }
        public async Task <IActionResult> Post(int id, [FromBody] Penalty penalty)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Penalties).FirstAsync(x => x.BroeCrewId == id);

            Models.Penalty dbPenalty = _mapper.Map <Models.Penalty>(penalty);
            crew.Penalties.Add(dbPenalty);
            await _context.SaveChangesAsync();

            return(CreatedAtRoute("GetByCrewAndId", new { id = crew.BroeCrewId, penaltyId = dbPenalty.PenaltyId }));
        }
예제 #6
0
        public async Task <IActionResult> GetByCrew(int id)
        {
            Models.Crew crew = await _context.Crews.Include("Awards.Award").FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <List <Models.Award>, List <Award> >(crew.Awards.Select(x => x.Award).ToList())));
        }
        public async Task <IActionResult> ListByCrew(int id)
        {
            Models.Crew crew = await _context.Crews.Include("Athletes.Athlete").FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <List <Models.CrewAthlete>, List <Athlete> >(crew.Athletes)));
        }
        public async Task <IActionResult> GetByCrew(int id)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Penalties).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <List <Models.Penalty>, List <Penalty> >(crew.Penalties)));
        }
예제 #9
0
        public async Task <IActionResult> GetByCrew(int id)
        {
            Models.Crew crew = await _context.Crews.Include("Results.TimingPoint")
                               .Include(c => c.Competition.TimingPoints).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            List <Result> viewResults = await GetResults(crew);

            return(Ok(viewResults));
        }
예제 #10
0
        public async Task <IActionResult> DeleteById(int id)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Competition.TimingPoints)
                               .Include(x => x.Results).Include(x => x.Penalties).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            _context.Crews.Remove(crew);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #11
0
        /// <summary>
        /// Gets a crew Role and adds it if it doesn't exist.
        /// </summary>
        /// <param name="department"></param>
        /// <param name="job"></param>
        /// <returns></returns>
        public Crew AddGetCrewRole(string department, string job)
        {
            Crew c  = new Models.Crew(department, job);
            Crew tc = Crew.Where(o => o.Department == department && o.Job == job).FirstOrDefault();

            if (tc != null)
            {
                c = tc;
            }
            else
            {
                c.CrewID = NextCrewIndex++;
                Crew.Add(c); // Add Crew to Universe
            }
            return(c);
        }
        public async Task <IActionResult> GetByCrewAndId(int id, int penaltyId)
        {
            Models.Crew crew = await _context.Crews.FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            Models.Penalty penalty = await _context.Penalties.FirstOrDefaultAsync(x => x.PenaltyId == penaltyId);

            if (penalty == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(_mapper.Map <Penalty>(penalty)));
            }
        }
        public async Task <IActionResult> DeleteByCrewAndId(int id, int penaltyId)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Penalties).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            Models.Penalty penalty = crew.Penalties.FirstOrDefault(x => x.PenaltyId == penaltyId);

            if (penalty == null)
            {
                return(NotFound());
            }

            _context.Penalties.Remove(penalty);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IActionResult> DeleteByCrewAndPosition(int id, int position)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Athletes).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            Models.CrewAthlete athlete = crew.Athletes.FirstOrDefault(x => x.Position == position);

            if (athlete == null)
            {
                return(NotFound());
            }

            crew.Athletes.Remove(athlete);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #15
0
        public async Task <IActionResult> DeleteByCrewAndTimingPoint(int id, int timingPointId)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Results).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            Models.Result result = crew.Results.FirstOrDefault(x => x.TimingPointId == timingPointId);

            if (result == null)
            {
                return(NotFound());
            }

            _context.Results.Remove(result);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #16
0
        public async Task <IActionResult> Put(int compid, int id, [FromBody] Crew crew)
        {
            Models.Competition competition = await _context.Competitions.Include("Administrators.CompetitionAdministrator").FirstAsync(x => x.CompetitionId == compid);

            var authorizationResult = await _authorizationHelper.AuthorizeAsync(User, competition, "CanAdminCompetition");

            if (!authorizationResult.Succeeded)
            {
                if (User.Identity.IsAuthenticated)
                {
                    return(new ForbidResult());
                }
                else
                {
                    return(new ChallengeResult());
                }
            }

            Models.Crew dbCrew = await _context.Crews.FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (dbCrew == null)
            {
                Models.Crew modelCrew = new Models.Crew {
                    Competition = competition
                };
                competition.Crews.Add(modelCrew);
                _mapper.Map(crew, modelCrew);
                await _context.SaveChangesAsync();

                return(CreatedAtRoute("GetById", new { id = modelCrew.BroeCrewId }));
            }
            else
            {
                _mapper.Map(crew, dbCrew);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
        }
예제 #17
0
        public async Task <IActionResult> Put(int id, int timingPointId, [FromBody] Result result)
        {
            Models.Crew crew = await _context.Crews.Include(x => x.Results).FirstAsync(x => x.BroeCrewId == id);

            Models.Result modelResult = crew.Results.FirstOrDefault(x => x.TimingPointId == timingPointId);
            if (modelResult != null)
            {
                _mapper.Map(result, modelResult);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            else
            {
                Models.Result dbResult = _mapper.Map <Models.Result>(result);
                dbResult.TimingPointId = timingPointId;
                crew.Results.Add(dbResult);
                await _context.SaveChangesAsync();

                return(CreatedAtRoute("GetByCrewAndTimingPoint", new { crewId = crew.BroeCrewId }));
            }
        }
예제 #18
0
        public async Task <IActionResult> Patch(int compid, int id, [FromBody] JsonPatchDocument <Crew> crewPatch)
        {
            if (crewPatch is null)
            {
                return(BadRequest());
            }

            Models.Crew dbCrew = await _context.Crews.FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (dbCrew.CompetitionId != compid)
            {
                return(BadRequest());
            }

            Crew crew = _mapper.Map <Crew>(dbCrew);

            crewPatch.ApplyTo(crew);
            _mapper.Map(crew, dbCrew);

            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #19
0
        public async Task <IActionResult> GetByCrewAndTimingPoint(int id, int timingPointId)
        {
            Models.Crew crew = await _context.Crews.Include("Results.TimingPoint")
                               .Include(c => c.Competition.TimingPoints).FirstOrDefaultAsync(x => x.BroeCrewId == id);

            if (crew == null)
            {
                return(NotFound());
            }

            List <Result> results = await GetResults(crew);

            Result response = results.FirstOrDefault(x => x.Id == timingPointId);

            if (response == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(response));
            }
        }
        public async Task <IActionResult> PutByCrewAndPosition(int id, int position, [FromBody] Athlete athlete)
        {
            if (athlete is null)
            {
                return(BadRequest());
            }

            Models.Crew crew = await _context.Crews.Include("Athletes.Athlete").FirstAsync(x => x.BroeCrewId == id);

            Models.CrewAthlete crewAthlete = crew.Athletes.FirstOrDefault(x => x.Position == position);

            bool created = false;

            if (crewAthlete == null)
            {
                Models.Athlete dbAthlete = await _context.Athletes.FirstOrDefaultAsync(x => x.MembershipNumber == athlete.MembershipNumber);

                if (dbAthlete == null)
                {
                    crew.Athletes.Add(_mapper.Map <Models.CrewAthlete>(athlete));
                    created = true;
                }
                else
                {
                    crewAthlete = new Models.CrewAthlete
                    {
                        Athlete = dbAthlete
                    };

                    crew.Athletes.Add(crewAthlete);
                    _mapper.Map(athlete, crewAthlete);
                }
            }
            else
            {
                if (crewAthlete.Athlete.MembershipNumber == athlete.MembershipNumber)
                {
                    _mapper.Map(athlete, crewAthlete);
                }
                else
                {
                    Models.Athlete dbAthlete = await _context.Athletes.FirstOrDefaultAsync(x => x.MembershipNumber == athlete.MembershipNumber);

                    crew.Athletes.Remove(crewAthlete);
                    if (dbAthlete == null)
                    {
                        crew.Athletes.Add(_mapper.Map <Models.CrewAthlete>(athlete));
                        created = true;
                    }
                    else
                    {
                        Models.CrewAthlete newCrewAthlete = new Models.CrewAthlete {
                            Athlete = dbAthlete
                        };
                        crew.Athletes.Add(newCrewAthlete);
                        _mapper.Map(athlete, newCrewAthlete);
                    }
                }
            }
            await _context.SaveChangesAsync();

            if (created)
            {
                return(CreatedAtRoute("GetByCrewAndPosition", new { id = crew.BroeCrewId, position = athlete.Position }));
            }
            else
            {
                return(NoContent());
            }
        }
        public static Api.Resources.Crew BuildCrew(IMapper mapper, Models.Competition competition, Models.Crew modelCrew)
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }
            if (competition is null)
            {
                throw new ArgumentNullException(nameof(competition));
            }
            if (modelCrew is null)
            {
                throw new ArgumentNullException(nameof(modelCrew));
            }

            TimingPoint startPoint = competition.TimingPoints.First();

            Api.Resources.Crew apiCrew = mapper.Map <Api.Resources.Crew>(modelCrew);

            foreach (Models.Result modelResult in modelCrew.Results)
            {
                Api.Resources.Result apiResult = new Api.Resources.Result()
                {
                    Id          = modelResult.TimingPointId,
                    TimeOfDay   = modelResult.TimeOfDay,
                    Name        = modelResult.TimingPoint.Name,
                    RunTime     = modelCrew.RunTime(startPoint.TimingPointId, modelResult.TimingPointId),
                    SectionTime = startPoint.TimingPointId != modelResult.TimingPointId ?
                                  modelCrew.RunTime(competition.TimingPoints[competition.TimingPoints.IndexOf(modelResult.TimingPoint) - 1].TimingPointId,
                                                    modelResult.TimingPointId) : null
                };
                apiCrew.Results.Add(apiResult);
            }

            return(apiCrew);
        }