Esempio n. 1
0
        public async Task <IActionResult> Edit([FromBody] EditTeamViewModel editTeamViewModel)
        {
            logger.LogInformation($"POST Request {HttpContext.Request.Headers[":path"]}. Body: {JsonConvert.SerializeObject(editTeamViewModel)}");

            var teamId = editTeamViewModel.Id;

            if (!await accessChecker.CanManageTeam(teamId))
            {
                throw new HttpStatusException(HttpStatusCode.Forbidden, CommonErrorMessages.Forbidden);
            }

            var team = await context.Teams.FirstOrDefaultAsync(t => t.Id == teamId);

            if (team == null)
            {
                throw new HttpStatusException(HttpStatusCode.BadRequest, TeamErrorMessages.NotFound, TeamErrorMessages.DebugNotFound(teamId));
            }

            var @event = await context.Events.FirstOrDefaultAsync(e => e.Id == editTeamViewModel.EventId);

            var config = new MapperConfiguration(cfg => cfg.CreateMap <EditTeamViewModel, Team>()
                                                 .ForMember("Event", opt => opt.MapFrom(_ => @event)));
            var mapper = new Mapper(config);

            mapper.Map(editTeamViewModel, team);

            try
            {
                context.Update(team);
                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw new HttpStatusException(HttpStatusCode.InternalServerError, CommonErrorMessages.SaveChanges);
            }

            return(Ok(team));
        }
Esempio n. 2
0
        public async Task <IActionResult> Delete(long id)
        {
            logger.LogInformation($"DELETE Request {HttpContext.Request.Headers[":path"]}.");

            if (!await accessChecker.CanManageTeam(id))
            {
                throw new HttpStatusException(HttpStatusCode.Forbidden, CommonErrorMessages.Forbidden);
            }

            var team = await context.Teams.FirstOrDefaultAsync(t => t.Id == id);

            if (team == null)
            {
                throw new HttpStatusException(HttpStatusCode.BadRequest, TeamErrorMessages.NotFound, TeamErrorMessages.DebugNotFound(id));
            }

            try
            {
                context.Remove(team);
                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw new HttpStatusException(HttpStatusCode.InternalServerError, CommonErrorMessages.SaveChanges);
            }

            return(Json("Deleted"));
        }
        public async Task <IActionResult> SetTeam(long id, long teamId, bool isTeamOffer = true)
        {
            logger.LogInformation($"POST Request {HttpContext.Request.Headers[":path"]}");

            var dbTeam = await context.Teams
                         .Include(x => x.Image)
                         .Include(x => x.UserTeams)
                         .ThenInclude(x => x.User)
                         .Include(x => x.UserTeams)
                         .ThenInclude(x => x.Team)
                         .ThenInclude(x => x.Event)
                         .FirstOrDefaultAsync(x => x.Id == teamId);

            if (dbTeam == null)
            {
                throw new HttpStatusException(HttpStatusCode.NotFound, TeamErrorMessages.NotFound, TeamErrorMessages.DebugNotFound(teamId));
            }

            var userActionToSet = isTeamOffer
                                ? UserActionEnum.ConsideringOffer
                                : UserActionEnum.SentRequest;

            if (dbTeam.UserTeams.All(x => x.UserId != id))
            {
                dbTeam.UserTeams.Add(new UserTeam {
                    UserId = id, UserAction = userActionToSet
                });
            }
            else
            {
                var user = dbTeam.UserTeams.FirstOrDefault(x => x.UserId == id);
                if (user == null)
                {
                    throw new HttpStatusException(HttpStatusCode.NotFound, UserErrorMessages.NotFound, UserErrorMessages.DebugNotFound(id));
                }

                user.UserAction = userActionToSet;
            }

            try
            {
                context.Update(dbTeam);
                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw new HttpStatusException(HttpStatusCode.InternalServerError, CommonErrorMessages.SaveChanges);
            }

            await SetTeamNotify(id, dbTeam, userActionToSet);

            return(Json(dbTeam));
        }