Beispiel #1
0
 public async Task<IHttpActionResult> CreateEvent(Event @event)
 {
     if (! await _service.CreateEventAsync(@event, User.Identity.Name))
     {
         return BadRequest(ModelState);
     }
     return CreatedAtRoute("CreateEventRoute", new { id = @event.ID }, @event);
 }
Beispiel #2
0
        public async Task<IHttpActionResult> UpdateEvent(int id, Event @event)
        {
            if (id != @event.ID)
            {
                return BadRequest();
            }

            if (! await _service.UpdateEventAsync(@event, User.Identity.Name))
            {
                return BadRequest(ModelState);
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Beispiel #3
0
        public async Task<bool> UpdateEventAsync(Event @event, string username)
        {
            // Validate not found
            var e = await _respository.GetAsync(@event.ID);
            if (e == null)
            {
                _validationDictionary.AddErrors("Event", "Not found");
                return false;
            }

            // Validate authorization
            var user = await _respository.GetMasterAsync(@event);
            if (!user.Username.Trim().Equals(username.Trim()))
            {
                _validationDictionary.AddErrors("Authorization", "User does not have permission.");
                return false;
            }

            // Validate logic
            if (!ValidateEvent(@event))
                return false;

            // Database logic
            try
            {
                await _respository.UpdateEventAsync(@event);
            }
            catch
            {
                return false;
            }

            //Create notification
            var list = _userRespository.GetJoinedUser(e);
            foreach (var item in list)
            {
                User u = new User
                {
                    ID = item.ID
                };
                await _notificationRespository.CreateNotificationAsync(u, "Hey guess what!!! " + e.Name + " has been updated, check it out now!");
            }

            return true;
        }
Beispiel #4
0
        public async Task<bool> CreateEventAsync(Event @event, string username)
        {
            // Validate logic
            if (!ValidateEvent(@event))
                return false;

            // Database logic
            try
            {
                var user = await _userRespository.GetByUsernameAsync(username);
                await _respository.CreateEventAsync(@event, user);
            }
            catch
            {
                return false;
            }
            return true;
        }
Beispiel #5
0
        public bool CreateEvent(Event @event, string username)
        {
            // Validate logic
            if (!ValidateEvent(@event))
                return false;

            // Database logic
            try
            {
                var user = _userRespository.GetByUsername(username);
                _respository.CreateEvent(@event, user);
            }
            catch
            {
                return false;
            }
            return true;
        }
Beispiel #6
0
        protected bool ValidateEvent(Event @event)
        {
            // Validate Name
            if (@event.Name.Trim().Length == 0)
                _validationDictionary.AddErrors("Name", "Name cannot be blank.");
            else if (@event.Name.Trim().Length > 50)
                _validationDictionary.AddErrors("Name", "Name cannot exceed 50 characters.");
            // Validate Info
            if (@event.Info.Trim().Length == 0)
                _validationDictionary.AddErrors("Info", "Info cannot be blank.");
            else if (@event.Info.Trim().Length > 150)
                _validationDictionary.AddErrors("Info", "Info cannot exceed 150 characters.");
            // Validate Time
            if (@event.Time == null)
                _validationDictionary.AddErrors("Time", "Time cannot be blank.");
            else if (@event.Time.CompareTo(DateTime.Today.AddDays(1)) < 0)
                _validationDictionary.AddErrors("Time", "Time is not valid.");
            // Validate Place
            if (@event.Place.Trim().Length == 0)
                _validationDictionary.AddErrors("Place", "Place cannot be blank.");
            else if (@event.Place.Trim().Length > 50)
                _validationDictionary.AddErrors("Place", "Place cannot exceed 50 characters.");
            // Validate MaxAttendance
            if (@event.MaxAttendance != null && @event.MaxAttendance < 0)
                _validationDictionary.AddErrors("MaxAttendance", "Max Attendance cannot be negative");
            // Validate RequireAttendance
            if (@event.RequireAttendance != null && @event.RequireAttendance < 0)
                _validationDictionary.AddErrors("RequireAttendance", "Required Attendance cannot be negative");
            // Validate MaxAttendance and RequireAttendance
            if (@event.RequireAttendance != null && @event.MaxAttendance != null
                && @event.MaxAttendance < @event.RequireAttendance)
                _validationDictionary.AddErrors("MaxAttendance", "Max Attendance cannot be less than Required Attendance");
            // Validate Vote
            if (@event.Vote != null && @event.Vote < 0)
                _validationDictionary.AddErrors("Vote", "Vote cannot be negative");
            // Validate Price
            if (@event.Price != null && @event.Price < 0)
                _validationDictionary.AddErrors("Price", "Price cannot be negative");
            // Validate Image

            // Validate Categories
            foreach (var item in @event.Categories)
            {
                var category = _categoryRespository.Get(item.ID);
                if (category == null)
                {
                    _validationDictionary.AddErrors("Categories", "Category is not valid");
                    break;
                }
            }

            return _validationDictionary.IsValid;
        }