Beispiel #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            DateTime birthDate = (DateTime)value;

            int age = DateUtil.CalculateAge(birthDate);

            // your validation logic
            if (age >= 18)
            {
                return(ValidationResult.Success);
            }
            else
            {
                return(new ValidationResult("You must be at least 18 years old"));
            }
        }
Beispiel #2
0
        public async Task <ActionResult> Attend(Guid id)
        {
            Event eventEntity = await UnitOfWork.Events.GetWithRelated(id, e => e.Category, e => e.Attendances);

            if (eventEntity == null)
            {
                throw new RestError(HttpStatusCode.BadRequest, new { Event = "Event doesn't exist." });
            }

            if (!eventEntity.IsActive)
            {
                throw new RestError(HttpStatusCode.BadRequest, new { Event = "Event isn't active." });
            }

            if (eventEntity.StartDate < DateTime.Now.AddHours(-1))
            {
                throw new RestError(HttpStatusCode.BadRequest, new { Event = "Too late to join this event." });
            }

            Guid attendanceId = Guid.Parse(HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);

            User user = await UnitOfWork.Users.Get(attendanceId);

            //check if user already attended
            if (eventEntity.Attendances.FirstOrDefault(a => a.AttendeeId == attendanceId) != null)
            {
                throw new RestError(HttpStatusCode.BadRequest, new { Event = "You are already attending this event." });
            }

            //check if user is host
            if (eventEntity.HostId == attendanceId)
            {
                throw new RestError(HttpStatusCode.BadRequest, new { Event = "As a host, you don't need the join your event." });
            }

            //check if event has age limit, and user is eligible
            if (eventEntity.MinAgeLimit.HasValue)
            {
                int age = DateUtil.CalculateAge(user.BirthDate);

                if (age < eventEntity.MinAgeLimit.Value)
                {
                    throw new RestError(HttpStatusCode.BadRequest, new { Event = $"This event has minimum age limit {eventEntity.MinAgeLimit.Value}, and you are {age} years old." });
                }
            }

            if (eventEntity.MaxAgeLimit.HasValue)
            {
                int age = DateUtil.CalculateAge(user.BirthDate);

                if (age > eventEntity.MaxAgeLimit.Value)
                {
                    throw new RestError(HttpStatusCode.BadRequest, new { Event = $"This event has maximum age limit {eventEntity.MaxAgeLimit.Value}, and you are {age} years old." });
                }
            }

            //check if event has already max number of people
            if (eventEntity.MaxNumberOfPeople.HasValue)
            {
                //check how many attendances for the event
                if (!(eventEntity.Attendances.Count < eventEntity.MaxNumberOfPeople.Value))
                {
                    throw new RestError(HttpStatusCode.BadRequest, new { Event = $"This event has already {eventEntity.MaxNumberOfPeople.Value} people." });
                }
            }

            Attendance attendance = new Attendance
            {
                CreatedAt  = DateTime.Now,
                AttendeeId = attendanceId,
                EventId    = id
            };

            UnitOfWork.Attendances.Add(attendance);

            await UnitOfWork.CompleteAsync();

            return(Ok());
        }