Exemplo n.º 1
0
        public void Update(CreateHangoutViewModel model)
        {
            var hangout = (from a in Hangout_db.Hangouts
                           where a.Id == model.Id
                           select a).Single();
            hangout.Date = model.Date;
            hangout.Description = model.Description;
            hangout.Name = model.Name;
            hangout.Address = model.Address;
            hangout.ContactInfo = model.ContactInfo;
            hangout.Location = model.Location;
            hangout.PartySize = model.PartySize;
            hangout.GenderRatio = model.GenderRatio;

            if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
            {
                using (var binaryReader = new BinaryReader(model.ImageUpload.InputStream))
                {
                    hangout.ImageContent = binaryReader.ReadBytes(model.ImageUpload.ContentLength);
                }
                hangout.ImageMimeType = model.ImageUpload.ContentType;
            }

            DateTime startTimeOut, endTimeOut;
            if (model.StartTime.HasValue)
            {
                hangout.StartTime = model.StartTime;
            }
            if (model.EndTime.HasValue)
            {
                hangout.EndTime = model.EndTime;
            }

            try
            {
                Hangout_db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // Provide for exceptions.
            }
        }
Exemplo n.º 2
0
        public void Create(CreateHangoutViewModel model)
        {
            Hangout hang = new Hangout
            {
                Date = model.Date,
                Description = model.Description,
                Name = model.Name,
                UserCreator = model.UserId,
                Location = model.Location,
                Address = model.Address,
                PartySize = model.PartySize,
                ContactInfo = model.ContactInfo,
                GenderRatio = model.GenderRatio,
                StartTime = model.StartTime,
                EndTime = model.EndTime,
                AttendeeCount = 1,
                IsCancelled = false,
                ImageContent = new byte[8],
                ImageMimeType = "0"
            };
            if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
            {
                using (var binaryReader = new BinaryReader(model.ImageUpload.InputStream))
                {
                    hang.ImageContent = binaryReader.ReadBytes(model.ImageUpload.ContentLength);
                }
                hang.ImageMimeType = model.ImageUpload.ContentType;
            }

            string gend = GetUserGender(model.UserId);
            if(gend.Equals("male"))
            {
                hang.MaleAttendingCount++;
            }
            else if(gend.Equals("female"))
            {
                hang.FemaleAttendingCount++;
            }

            //Need to grab user in controller
            Hangout_db.Hangouts.InsertOnSubmit(hang);

            try
            {
                Hangout_db.SubmitChanges();
                // TODO: Add log entry for create event.
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // Make some adjustments.
                // ...
                // Try again.
                Hangout_db.SubmitChanges();
            }

            ActivityLog actLog = new ActivityLog
            {
                AspNetUserId = model.UserId,
                HangoutId = hang.Id,
                TimeStamp = DateTime.UtcNow,
                ActivityType = (int) ActivityType.Create
            };

            ActivityLog_db.ActivityLogs.InsertOnSubmit(actLog);

            try
            {
                ActivityLog_db.SubmitChanges();
                // TODO: Add log entry for create event.
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // Make some adjustments.
                // ...
                // Try again.
                ActivityLog_db.SubmitChanges();
            }
        }
Exemplo n.º 3
0
        public ActionResult Edit(CreateHangoutViewModel model)
        {
            JsonResult result = null;
            var errors = new List<string>();

            //Try to grab any model state errors
            if (!ModelState.IsValid)
            {
                var errorList = ModelState.ToDictionary(
                    kvp => kvp.Key,
                    kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToList());

                foreach (var error in errorList)
                {
                    foreach (string modelStateError in error.Value)
                    {
                        var message = string.Format("{0}: {1}", error.Key, modelStateError);
                        errors.Add(message);
                    }
                }
            }
            else
            {
                //Custom validation if we dont see any model state errors
                if (!model.StartTime.HasValue)
                {
                    ModelState.AddModelError(string.Empty, model.StartTime + " is an invalid time. E.g. 10:00am");
                    errors.Add(model.StartTime + " is an invalid time. E.g. 10:00am");
                }
                if (!model.EndTime.HasValue)
                {
                    ModelState.AddModelError(string.Empty, model.EndTime + " is an invalid time. E.g. 3:00pm");
                    errors.Add(model.EndTime + " is an invalid time. E.g. 3:00pm");
                }
                if (!model.Date.HasValue || (model.Date.Value.Date < DateTime.Today))
                {
                    ModelState.AddModelError(string.Empty, "Date cannot be paste due.");
                    errors.Add("Date cannot be paste due.");
                }

                if (!ModelState.IsValid && (model.StartTime.GetValueOrDefault() < model.EndTime.GetValueOrDefault()))
                {
                    ModelState.AddModelError(string.Empty, "End Time must be later than Start Time");
                    errors.Add("End Time must be later than Start Time");
                }
            }

            //Return errors if we have any
            if (errors.Any())
            {
                result = new JsonResult
                {
                    Data = new
                    {
                        Errors = errors
                    }
                };
                return result;
            }

            //Save if we dont have errors
            _hangoutRepository.Update(model);

            var userId = User.Identity.GetUserId();
            var hangoutViewModel = _hangoutRepository.GetHangoutViewModelById(userId, model.Id);

            //Return updated event card if everything is good
            result = new JsonResult
            {
                Data = new
                {
                    Errors = hangoutViewModel == null ? new List<string> {"Unable to find the hangout"} : new List<string>(),
                    View = hangoutViewModel != null ? ViewToString("_UpcomingHangoutPartial", hangoutViewModel) : ""
                }
            };
            return result;
        }
Exemplo n.º 4
0
        public async Task<ActionResult> Create(CreateHangoutViewModel model)
        {
            /*bool startTimeIsValid, endTimeIsValid;
            startTimeIsValid = IsValidTime(model.StartTime);
            endTimeIsValid = IsValidTime(model.EndTime);*/


            if (!model.StartTime.HasValue)
            {
                ModelState.AddModelError(string.Empty, model.StartTime + " is an invalid time. E.g. 10:00am");
            }
            if (!model.EndTime.HasValue)
            {
                ModelState.AddModelError(string.Empty, model.EndTime + " is an invalid time. E.g. 3:00pm");
            }

            if (!model.Date.HasValue)
            {
                ModelState.AddModelError("Date", "Date cannot be empty.");
            }
            if (model.Date < DateTime.Today)
            {
                ModelState.AddModelError("Date", "Date cannot be paste due.");
            }
            if (model.StartTime > model.EndTime)
            {
                ModelState.AddModelError(string.Empty, "End Time must be later than Start Time");
            }

            if (ModelState.IsValid)
            {
                var userInfo = User.Identity.GetUserId();
                model.UserId = userInfo;
                _hangoutRepository.Create(model);
                
                
                return RedirectToAction("MyHangouts");

                //var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                //var result = await UserManager.CreateAsync(user, model.Password);
                //if (result.Succeeded)
                //{
                //    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                //    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                //    // Send an email with this link
                //    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //    return RedirectToAction("Index", "Home");
                //}
                //AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 5
0
        public ActionResult Edit(int id)
        {
            var item = _hangoutRepository.GetHangoutById(id);
            var model = new CreateHangoutViewModel
            {
                Id = item.Id,
                Date = item.Date,
                Description = item.Description,
                Name = item.Name,
                UserId = item.UserCreator,
                Location = item.Location,
                Address = item.Address,
                ContactInfo = item.ContactInfo,
                PartySize = item.PartySize,
                GenderRatio = item.GenderRatio,
                StartTime = item.StartTime,
                EndTime = item.EndTime,
                ImageContent = item.ImageContent == null ? new byte[8] : item.ImageContent.ToArray(),
                ImageMimeType = item.ImageMimeType == null ? "none" : item.ImageMimeType

            };
            return View(model);
        }