Exemplo n.º 1
0
        public async Task <ActionResult <Models.HotOpSession> > UpdateHotOpSession(int hotOpId, int hotOpSessionId, [FromBody] Models.HotOpSession hotOpSessionModel)
        {
            try
            {
                var hotOpEntity = await Context.HotOps
                                  .Include(ho => ho.Sessions)
                                  .SingleOrDefaultAsync(ho => ho.Id == hotOpId);

                if (hotOpEntity == null)
                {
                    return(NotFound(new { message = $"Hot op {hotOpId} not found" }));
                }

                var hotOpSessionEntity = hotOpEntity.Sessions.FirstOrDefault(hos => hos.Id == hotOpSessionId);
                if (hotOpSessionEntity == null)
                {
                    return(NotFound(new { message = $"Hot op session {hotOpSessionId} not found" }));
                }

                hotOpSessionEntity.StartDateTimeUtc = hotOpSessionModel.StartDateTimeUtc;
                hotOpSessionEntity.EndDateTimeUtc   = hotOpSessionModel.EndDateTimeUtc;
                await Context.SaveChangesAsync();

                return(Ok(hotOpSessionModel));
            }
            catch (Exception ex)
            {
                return(InternalServerError($"Failed to update session {hotOpSessionId}", ex));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Models.HotOpSession> > CreateHotOpSession(int hotOpId, [FromBody] Models.HotOpSession hotOpSessionModel)
        {
            try
            {
                var hotOp = await Context.HotOps
                            .Include(ho => ho.Sessions)
                            .SingleOrDefaultAsync(ho => ho.Id == hotOpId);

                if (hotOp == null)
                {
                    return(NotFound(new { message = $"Hot op {hotOpId} not found" }));
                }
                var hotOpSessionEntity = Mapper.Map <Models.HotOpSession, Entities.HotOpSession>(hotOpSessionModel);
                hotOp.Sessions.Add(hotOpSessionEntity);
                await Context.SaveChangesAsync();

                hotOpSessionModel.Id = hotOpSessionEntity.Id;
                return(Ok(hotOpSessionModel));
            }
            catch (Exception ex)
            {
                return(InternalServerError($"Failed to create session for hot op {hotOpId}", ex));
            }
        }