Example #1
0
        public HttpResponseMessage CreateEvent(CreateEventsRequest request)
        {
            try
            {
                //Authenticate API key
                long?hostId = HostServices.GetCallerHostId();
                if (!hostId.HasValue)
                {
                    Log.Warn(ResponseMessages.InvalidAPIKey);
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidAPIKey, ResponseMessages.InvalidAPIKey)));
                }

                //Validate input
                if (request == null ||
                    string.IsNullOrEmpty(request.ReferenceId) ||
                    string.IsNullOrEmpty(request.Title) ||
                    !request.StartDate.HasValue ||
                    request.Tiers == null ||
                    request.Tiers.Count == 0)
                {
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, ResponseMessages.InvalidParam)));
                }

                //Validate input length
                if (request.ReferenceId.Length > 30)
                {
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, "Reference ID (max 30 length) is too long.")));
                }
                if (request.Title.Length > 200 || request.Description.Length > 500)
                {
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, "Title (max 200 length) or Description (max 500 length) is too long.")));
                }
                if (request.Tiers.Count > 10)
                {
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, "Only a maximum of 10 tiers allowed.")));
                }

                //Ensure that start date is in future
                if (DateTime.UtcNow.Date > request.StartDate.Value)
                {
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, "Event start date must be a future date.")));
                }

                //Validate event reference ID
                IdentityServices identityService = new IdentityServices();
                if (identityService.IsRefIdExists(IdentityServices.RefIdTypes.Event, hostId.Value, request.ReferenceId))
                {
                    return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.DuplicateRefId, ResponseMessages.DuplicateRefId + " (Event " + request.ReferenceId + ")")));
                }
                //Validate event tier reference ID and input length
                foreach (var tier in request.Tiers)
                {
                    if (tier.ReferenceId.Length > 30)
                    {
                        return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, "Reference ID (max 30 length) is too long.")));
                    }
                    if (identityService.IsRefIdExists(IdentityServices.RefIdTypes.EventTier, hostId.Value, tier.ReferenceId))
                    {
                        return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.DuplicateRefId, ResponseMessages.DuplicateRefId + " (Tier " + tier.ReferenceId + ")")));
                    }
                    if (tier.Title.Length > 200)
                    {
                        return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.InvalidParam, "Tier Title (max 200 length) is too long.")));
                    }
                }

                //Perform transaction
                EventsServices eventsService = new EventsServices();
                eventsService.CreateEvent(hostId.Value, request);

                //Send response
                return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.Success, ResponseMessages.Success)));
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                return(Request.CreateResponse(new GenericResponse(null, ResponseCodes.Error, ResponseMessages.Error)));
            }
        }