Пример #1
0
        private List <Role> GetUserRoles(ApplicationUser user, EventSpotDbContext db)
        {
            //Create user manager
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            //Get all application roles
            var roles = db.Roles
                        .Select(r => r.Name)
                        .OrderBy(r => r)
                        .ToList();

            //For each application role, check if the user has it
            var userRoles = new List <Role>();

            foreach (var roleName in roles)
            {
                var role = new Role {
                    Name = roleName
                };

                if (userManager.IsInRole(user.Id, roleName))
                {
                    role.IsSelected = true;
                }

                userRoles.Add(role);
            }

            //Return a list with all roles
            return(userRoles);
        }
        private void SetEventTags(Event events, EventViewModel model, EventSpotDbContext database)
        {
            //Split tags
            var tagsSplitter = model.Tags
                               .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            //Clear current article tags
            events.Tags.Clear();
            //Set new article tags
            foreach (var tagString in tagsSplitter)
            {
                //Get tag from db by its name
                Tag tag = database.Tags.FirstOrDefault(t => t.Name.Equals(tagString));
                //if the tag is null,create new tag
                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = tagString
                    };
                    database.Tags.Add(tag);
                }
                //Add tag to article tags
                events.Tags.Add(tag);
            }
        }
        public ActionResult Edit(EventViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var database = new EventSpotDbContext())
                {
                    //Get article from database
                    var events = database.Events
                                 .FirstOrDefault(a => a.Id == model.Id);

                    events.EventName        = model.EventName;
                    events.EventDate        = model.EventDate;
                    events.StartTime        = model.StartTime;
                    events.EventDescription = model.EventDescription;
                    events.CategoryId       = model.CategoryId;
                    events.CityId           = model.CityId;
                    this.SetEventTags(events, model, database);
                    database.Entry(events).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("Main"));
                }
            }
            return(View(model));
        }
Пример #4
0
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new EventSpotDbContext())
            {
                //Get user from database
                var user = database.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                //Get user articles from database
                var userEvents = database.Events
                                 .Where(a => a.Organizer.Id == user.Id);

                foreach (var events in userEvents)
                {
                    database.Events.Remove(events);
                }

                //Delete user and save changes
                database.Users.Remove(user);
                database.SaveChanges();

                return(RedirectToAction("List"));
            }
        }
        //
        //GET: Event/Delete
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new EventSpotDbContext())
            {
                var events = database.Events
                             .Where(a => a.Id == id)
                             .Include(a => a.Organizer)
                             .Include(a => a.Category)
                             .Include(a => a.City)
                             .First();

                if (!IsOrganizerAuthorizedToEdit(events))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                if (User.IsInRole("Attendant"))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                ViewBag.TagsString = string.Join(", ", events.Tags.Select(t => t.Name));

                if (events == null)
                {
                    return(HttpNotFound());
                }
                return(View(events));
            }
        }
 public ActionResult GetNavbarCities()
 {
     using (var db = new EventSpotDbContext())
     {
         var navbarCities = db.Cities.ToList();
         return(PartialView("_NavbarCitiesDropdown", navbarCities));
     }
 }
        public ActionResult List()
        {
            using (var database = new EventSpotDbContext())
            {
                var cities = database.Cities
                             .ToList();

                return(View(cities));
            }
        }
        public ActionResult Create(EventViewModel model)
        {
            if (ModelState.IsValid)
            {
                // To convert the user uploaded Photo as Byte Array before save to DB
                byte[] imageData = null;
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase poImgFile = Request.Files["Event"];

                    using (var binary = new BinaryReader(poImgFile.InputStream))
                    {
                        imageData = binary.ReadBytes(poImgFile.ContentLength);
                    }
                }

                //insert event in DB
                using (var database = new EventSpotDbContext())
                {
                    //Get OrganizerID
                    var organizerId = database.Users
                                      .Where(u => u.UserName == this.User.Identity.Name)
                                      .First()
                                      .Id;

                    var events = new Event(organizerId, model.EventName,
                                           model.EventDescription, model.EventDate,
                                           model.StartTime, model.CategoryId, model.CityId);


                    this.SetEventTags(events, model, database);


                    //Set Event Organizer
                    events.OrganizerId = organizerId;



                    events.Attends = 1;

                    events.EventPhoto = imageData;


                    //Save event in DB

                    database.Events.Add(events);
                    database.SaveChanges();

                    return(RedirectToAction("Main"));
                }
            }

            return(View(model));
        }
 //
 //GET: Event/List
 public ActionResult List()
 {
     using (var database = new EventSpotDbContext())
     {
         //Get Events from DB
         var events = database.Events
                      .Include(o => o.Organizer)
                      .Include(o => o.Tags)
                      .ToList();
         return(View(events));
     }
 }
Пример #10
0
        // GET: User/List
        public ActionResult List()
        {
            using (var database = new EventSpotDbContext())
            {
                var users = database.Users
                            .ToList();
                var admins = GetAdminUserNames(users, database);
                ViewBag.Admins = admins;

                return(View(users));
            }
        }
        public ActionResult ListCities()
        {
            using (var database = new EventSpotDbContext())
            {
                var cities = database.Cities
                             .Include(c => c.Events)
                             .OrderBy(c => c.Name)
                             .ToList();

                return(View(cities));
            }
        }
        //
        //Get: Event/Edit

        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new EventSpotDbContext())
            {
                //Get event from database
                var events = database.Events
                             .Where(a => a.Id == id)
                             .Include(a => a.Organizer)
                             .First();

                if (!IsOrganizerAuthorizedToEdit(events))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                if (User.IsInRole("Attendant"))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }
                //Check if event exists
                if (events == null)
                {
                    return(HttpNotFound());
                }

                //Create the view model
                var model = new EventViewModel();
                model.Id               = events.Id;
                model.EventName        = events.EventName;
                model.EventDate        = events.EventDate;
                model.StartTime        = events.StartTime;
                model.EventDescription = events.EventDescription;
                model.CategoryId       = events.CategoryId;
                model.Categories       = database.Categories
                                         .OrderBy(c => c.Name)
                                         .ToList();
                model.CityId = events.CityId;
                model.Cities = database.Cities
                               .OrderBy(c => c.Name)
                               .ToList();
                model.Tags = string.Join(", ", events.Tags.Select(t => t.Name));

                //Pass the view model to view
                return(View(model));
            }
        }
        public ActionResult Edit(City city)
        {
            if (ModelState.IsValid)
            {
                using (var database = new EventSpotDbContext())
                {
                    database.Entry(city).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(city));
        }
        public ActionResult Create(City city)
        {
            if (ModelState.IsValid)
            {
                using (var database = new EventSpotDbContext())
                {
                    database.Cities.Add(city);
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(city));
        }
        public ActionResult ListEventsByCity(int?cityId)
        {
            if (cityId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            using (var database = new EventSpotDbContext())
            {
                var events = database.Events
                             .Where(a => a.CityId == cityId)
                             .Include(a => a.Organizer)
                             .ToList();

                return(View(events));
            }
        }
Пример #16
0
        private HashSet <string> GetAdminUserNames(List <ApplicationUser> users, EventSpotDbContext context)
        {
            var userManager = new UserManager <ApplicationUser>(
                new UserStore <ApplicationUser>(context));

            var admins = new HashSet <string>();

            foreach (var user in users)
            {
                if (userManager.IsInRole(user.Id, "Admin"))
                {
                    admins.Add(user.UserName);
                }
            }

            return(admins);
        }
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            using (var database = new EventSpotDbContext())
            {
                var city = database.Cities
                           .FirstOrDefault(c => c.Id == id);

                if (city == null)
                {
                    return(HttpNotFound());
                }
                return(View(city));
            }
        }
 // GET: Tag
 public ActionResult List(int?id)
 {
     if (id == null)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     using (var database = new EventSpotDbContext())
     {
         //Get articles from db
         var events = database.Tags
                      .Include(t => t.Events.Select(a => a.Tags))
                      .Include(t => t.Events.Select(a => a.Organizer))
                      .FirstOrDefault(t => t.Id == id)
                      .Events
                      .ToList();
         return(View(events));
     }
 }
        public ActionResult Attend(int?id)
        {
            using (var database = new EventSpotDbContext())
            {
                //Get article from database
                var events = database.Events
                             .FirstOrDefault(a => a.Id == id);

                if (events == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                events.Attends += 1;
                database.SaveChanges();
            }
            return(Redirect(Request.UrlReferrer.PathAndQuery));
        }
Пример #20
0
        private void SetUserRoles(ApplicationUser user, EventSpotDbContext context, EditUserViewModel model)
        {
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            foreach (var role in model.Roles)
            {
                if (role.IsSelected)
                {
                    userManager.AddToRole(user.Id, role.Name);
                }
                else if (!role.IsSelected)
                {
                    userManager.RemoveFromRole(user.Id, role.Name);
                }
            }
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            using (var database = new EventSpotDbContext())
            {
                var events = database.Events
                             .Where(a => a.Id == id)
                             .Include(a => a.Organizer)
                             .First();


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

                database.Events.Remove(events);
                database.SaveChanges();

                return(RedirectToAction("Main"));
            }
        }
        public ActionResult Create()
        {
            using (var database = new EventSpotDbContext())
            {
                if (User.IsInRole("Attendant"))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                var model = new EventViewModel();
                model.Categories = database.Categories
                                   .OrderBy(c => c.Name)
                                   .ToList();

                model.Cities = database.Cities
                               .OrderBy(c => c.Name)
                               .ToList();

                return(View(model));
            }
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            using (var database = new EventSpotDbContext())
            {
                var city = database.Cities
                           .FirstOrDefault(c => c.Id == id);

                var cityEvents = city.Events
                                 .ToList();

                foreach (var events in cityEvents)
                {
                    database.Events.Remove(events);
                }

                database.Cities.Remove(city);
                database.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
Пример #24
0
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            //Check if model is valid
            if (ModelState.IsValid)
            {
                using (var database = new EventSpotDbContext())
                {
                    //Get user from databese
                    var user = database.Users.FirstOrDefault(u => u.Id == id);

                    //Check if user exists
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    //If password field is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    //Set user properties
                    user.Email    = viewModel.User.Email;
                    user.FullName = viewModel.User.FullName;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(user, database, viewModel);

                    //Save changes
                    database.Entry(user).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }
            return(View(viewModel));
        }
        //
        //GET: Event/Details
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new EventSpotDbContext())
            {
                //Get Events from DB
                var events = database.Events
                             .Where(e => e.Id == id)
                             .Include(o => o.Organizer)
                             .Include(o => o.Tags)
                             .First();

                if (events == null)
                {
                    return(HttpNotFound());
                }
                return(View(events));
            }
        }
Пример #26
0
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new EventSpotDbContext())
            {
                //Get user from database
                var user = database.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                //Check if user exists
                if (user == null)
                {
                    return(HttpNotFound());
                }

                return(View(user));
            }
        }
Пример #27
0
        //GET: User/Edit
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            using (var database = new EventSpotDbContext())
            {
                var user = database.Users
                           .Where(u => u.Id == id)
                           .First();

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

                var viewModel = new EditUserViewModel();
                viewModel.User  = user;
                viewModel.Roles = GetUserRoles(user, database);

                return(View(viewModel));
            }
        }
 public AccountController()
 {
     context = new EventSpotDbContext();
 }