Beispiel #1
0
        public async Task <ActionResult> Edit(Friend friend)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var context = new S2GamesContext())
                    {
                        context.Friends.AddOrUpdate(friend);

                        await context.SaveChangesAsync();

                        context.Dispose();
                        return(RedirectToAction("Index"));
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TransportData();
            }

            return(View());
        }
Beispiel #2
0
        public async Task <ActionResult> DeleteConfirmed(int id = 0)
        {
            try
            {
                using (var context = new S2GamesContext())
                {
                    var repository = new GameRepository(context);

                    var game = await repository.GetByIdAsync(id, ConnectedId);

                    if (game == null)
                    {
                        return(HttpNotFound());
                    }

                    context.Games.Remove(game);

                    await context.SaveChangesAsync();

                    context.Dispose();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TransportData();
            }

            return(RedirectToAction("Index"));
        }
Beispiel #3
0
        public async Task <ActionResult> Edit(int id = 0)
        {
            if (id > 0)
            {
                using (var context = new S2GamesContext())
                {
                    var repository = new FriendRepository(context);

                    var friend = await repository.GetByIdAsync(id, ConnectedId);

                    if (friend == null)
                    {
                        return(HttpNotFound());
                    }

                    context.Dispose();
                    return(View(friend));
                }
            }
            else
            {
                return(View(new Friend {
                    UserId = ConnectedId
                }));
            }
        }
Beispiel #4
0
        public async Task <ActionResult> Connect(string username, string password)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (var context = new S2GamesContext())
                    {
                        var repository = new UserRepository(context);

                        int accountId = 0;
                        accountId = await repository.ConnectAsync(username, password);

                        ConnectedId = accountId;

                        if (accountId > 0)
                        {
                            return(RedirectToAction("Index", "Game"));
                        }
                        else
                        {
                            throw new Exception("Usuário ou senha incorretos");
                        }
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                    TransportData();
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #5
0
        public async Task <ActionResult> Delete(int id = 0)
        {
            using (var context = new S2GamesContext())
            {
                var repository = new GameRepository(context);

                var game = await repository.GetByIdAsync(id, ConnectedId);

                if (game == null)
                {
                    return(HttpNotFound());
                }

                context.Dispose();
                return(View(game));
            }
        }
Beispiel #6
0
        public async Task <ActionResult> Index(int?page, string search = null)
        {
            try
            {
                using (var context = new S2GamesContext())
                {
                    IQueryable <Game> query = null;

                    if (search == null)
                    {
                        query = context.Games
                                .Where(g => g.UserId == ConnectedId)
                                .Include(g => g.LentFor);
                    }
                    else
                    {
                        query = context.Games
                                .Where(g => (g.Label.Contains(search) || g.LentFor.Name.Contains(search)) && g.UserId == ConnectedId)
                                .Include(g => g.LentFor);
                    }

                    var games = await query.ToListAsync();

                    var pageNumber = page ?? 1;
                    var friends    = await context.Friends
                                     .Where(f => f.UserId == ConnectedId)
                                     .ToListAsync();

                    ViewBag.GamesPagedList = games.ToPagedList(pageNumber, ItemsPerPage);
                    ViewBag.Search         = search;
                    ViewBag.Friends        = new SelectList(friends, "Id", "Name");

                    context.Dispose();
                    return(View());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TransportData();
            }

            return(View());
        }
Beispiel #7
0
        public async Task <ActionResult> Edit(int id = 0)
        {
            if (id > 0)
            {
                using (var context = new S2GamesContext())
                {
                    var repository = new GameRepository(context);
                    var game       = await repository.GetByIdAsync(id, ConnectedId);

                    context.Dispose();
                    return(View(game));
                }
            }
            else
            {
                return(View(new Game {
                    UserId = ConnectedId
                }));
            }
        }
Beispiel #8
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session["ConnectedId"] == null)
            {
                filterContext.HttpContext.Session.Abandon();
                if (ThrowExceptions)
                {
                    throw new HttpException("Usuário desconectado");
                }
                else
                {
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                        { "Controller", "Home" }, { "Action", "Index" }
                    });
                }
            }
            else
            {
                var id = (int?)filterContext.HttpContext.Session["ConnectedId"];

                var context    = new S2GamesContext();
                var repository = new UserRepository(context);

                var user = repository.GetById(id);
                if (user == null)
                {
                    if (ThrowExceptions)
                    {
                        throw new HttpException("Usuário inválido");
                    }
                    else
                    {
                        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                            { "Controller", "Home" }, { "Action", "Index" }
                        });
                    }
                }
            }
        }
Beispiel #9
0
        public async Task <ActionResult> DeleteConfirmed(int id = 0)
        {
            try
            {
                using (var context = new S2GamesContext())
                {
                    var repository = new FriendRepository(context);
                    await repository.DeleteAsync(id, ConnectedId);

                    await context.SaveChangesAsync();

                    context.Dispose();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TransportData();
            }

            return(RedirectToAction("Index"));
        }
Beispiel #10
0
        public async Task <ActionResult> Index(int?page, string search = null)
        {
            try
            {
                using (var context = new S2GamesContext())
                {
                    IQueryable <Friend> query = null;

                    if (search == null)
                    {
                        query = context.Friends
                                .Where(f => f.UserId == ConnectedId);
                    }
                    else
                    {
                        query = context.Friends
                                .Where(f => f.Name.Contains(search) && f.UserId == ConnectedId);
                    }

                    var friends = await query.ToListAsync();

                    var pageNumber = page ?? 1;

                    ViewBag.FriendsPagedList = friends.ToPagedList(pageNumber, ItemsPerPage);
                    ViewBag.Search           = search;

                    context.Dispose();
                    return(View());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TransportData();
            }

            return(View());
        }
Beispiel #11
0
        public async Task <ActionResult> ReturnGame(int gameId)
        {
            try
            {
                using (var context = new S2GamesContext())
                {
                    var repository = new GameRepository(context);
                    await repository.LendOrReturn(null, gameId, ConnectedId);

                    await context.SaveChangesAsync();

                    context.Dispose();

                    Response.StatusCode = (int)HttpStatusCode.OK;
                    return(Json("Devolvido com sucesso", JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(ex.Message, JsonRequestBehavior.AllowGet));
            }
        }
Beispiel #12
0
 public UserRepository(S2GamesContext context)
 {
     this.Context = context;
 }
Beispiel #13
0
 public FriendRepository(S2GamesContext context)
 {
     this.Context = context;
 }