public async Task <ActionResult <SessionDto> > GetSession(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var session = await sessionService.GetSessionDtoAsync(id, SessionDto.IncludeNavigations());

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

            return(session);
        }
Beispiel #2
0
        public async Task <ActionResult <IEnumerable <SessionDto> > > GetSessions(string searchText = null
                                                                                  /*, int pageNumber=1, int pageSize=7*/)
        {
            // var sessions = _context.Sessions.Select(SessionDto.AsSessionDto);
            List <Expression <Func <SessionDto, bool> > > filters = null;

            if (String.IsNullOrEmpty(searchText)

                )
            {
                // return null;
            }
            else
            {
                filters = new List <Expression <Func <SessionDto, bool> > >();

                if (!String.IsNullOrEmpty(searchText))
                {
                    if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0)
                    {
                        filters.Add(x => x.Id.ToString().Contains(searchText));
                    }
                }
            }

            //sort
            //return sessions.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize);

            // OnSelectQuery(ref sessions);

            // return await sessions.ToListAsync();

            if (filters == null)
            {
                return(await sessionService.GetSessionDtoesAsync(SessionDto.IncludeNavigations()));
            }
            else
            {
                return(await sessionService.GetSessionDtoesAsync(SessionDto.IncludeNavigations(), filters.ToArray()));
            }
        }
Beispiel #3
0
        public async Task <bool> UpdateSessionAsync(SessionDto sessionDto, string username /*, String[] includeNavigations, params Expression<Func<Session, bool>>[] filters*/)
        {
            OnUpdate(sessionDto, username);

            // Get Session
            var entity = EntityQuery(_context, SessionDto.IncludeNavigations())
                         .FirstOrDefault(x => x.Id == sessionDto.Id);

            if (entity != null)
            {
                entity = SessionDto.ToSessionFunc(entity, sessionDto);

                ToEntity(ref entity, sessionDto);
                //entity.UpdateUser = entity.LastActivityUser = username;
                //entity.UpdateDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
                entity.EditTracker(username);

                OnBeforeUpdate(entity, username);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                    throw new Exception("Update error", ex);
                }
                finally
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                }
                OnAfterUpdate(entity, username);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        // -DtoQuery

        public async Task <SessionDto> CreateSessionAsync(SessionDto sessionDto, string username)
        {
            if (_context.Sessions.Any(a => a.Name == sessionDto.Name) == true)
            {
                throw new Exception("Record exist and caused a conflict!");
            }

            OnCreate(sessionDto, username);

            var entity = SessionDto.AsSessionFunc(sessionDto);

            ToEntity(ref entity, sessionDto);
            //entity.InsertUser = entity.LastActivityUser = username;
            //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
            entity.AddTracker(username);

            _context.Sessions.Add(entity);

            OnBeforeCreate(entity, username);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // _context.Entry(entity).State = EntityState.Detached;
                throw new Exception("Add error", ex);
            }
            finally
            {
                // _context.Entry(entity).State = EntityState.Detached;
            }
            OnAfterCreate(entity, username);

            // sessionDto = SessionDto.AsSessionDtoFunc(entity);
            sessionDto = await GetSessionDtoAsync(entity.Id, SessionDto.IncludeNavigations());

            return(sessionDto);
        }