Esempio n. 1
0
        public HttpResponseMessage AddEvents(AddEventRequestModel request)
        {
            if (request == null)
            {
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = "Request is empty.",
                    Status = HttpStatusCode.BadRequest
                }));
            }

            var dbPlayerResponse = AccountHelper.ExistsWithId(request.CreatorId);

            if (dbPlayerResponse == null ||
                dbPlayerResponse.Status != HttpStatusCode.OK)
            {
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = "No user found with this Id",
                    Status = HttpStatusCode.BadRequest
                }));
            }

            DateTime start;

            if (!DateTime.TryParse(request.StartDate, out start))
            {
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = "Start date is wrong.",
                    Status = HttpStatusCode.BadRequest
                }));
            }

            DateTime end = start;

            if (!string.IsNullOrEmpty(request.EndDate) &&
                !DateTime.TryParse(request.EndDate, out end))
            {
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = "End date is wrong.",
                    Status = HttpStatusCode.BadRequest
                }));
            }

            var dbResponse = EventsHelper.AddEvent(new Event
            {
                created_date = DateTime.Now,
                creator_id   = request.CreatorId,
                end_date     = end,
                start_date   = start,
                titre        = request.Titre,
                description  = request.Description
            });

            if (dbResponse == null)
            {
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = "An Error has occured.",
                    Status = HttpStatusCode.InternalServerError
                }));
            }

            switch (dbResponse.Status)
            {
            case HttpStatusCode.OK:
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = "OK",
                    Status = HttpStatusCode.OK
                }));

            default:
                return(ToolsBoxResponse.OK(new BasicResponseModel
                {
                    Message = dbResponse.Message,
                    Status = dbResponse.Status
                }));
            }
        }