public static JsonEvent FromDatabase(RaceDay.Models.Event iEvent) { return(new JsonEvent { EventId = iEvent.EventId, Name = iEvent.Name, Date = iEvent.Date, Url = iEvent.Url, Location = iEvent.Location, Description = iEvent.Description, CreatorId = iEvent.CreatorId, }); }
public RaceDay.Models.Event ToDatabase() { RaceDay.Models.Event iEvent = new RaceDay.Models.Event { EventId = EventId, Name = Name, Date = Date, Url = Url, Location = Location, Description = Description, CreatorId = CreatorId }; return(iEvent); }
// POST api/<controller> // // Create a new event. Json eventInfo is passed in through the body of the POST // public HttpResponseMessage Post([FromBody] JsonEvent eventInfo) { // Make sure the request is valid // if (string.IsNullOrEmpty(UserId)) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Not Authorized")); } if (eventInfo == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "Invalid Event Information")); } // Create new Model Event // RaceDay.Models.Event iEvent = eventInfo.ToDatabase(); iEvent.GroupId = GroupId; iEvent.CreatorId = UserId; // Add to the database and then add this user as a participant // Repository repository = new Repository(); Event newEvent = repository.AddEvent(iEvent); repository.SaveChanges(); if ((newEvent != null) && (newEvent.EventId > 0)) { var user = repository.GetUserById(UserId); repository.AddUserToEvent(user, newEvent, AttendingEnum.Attending); repository.SaveChanges(); var addedEvent = repository.GetEventViewById(newEvent.EventId, UserId); var eventAttendees = repository.GetUsersForEvent(newEvent.EventId); return(Request.CreateResponse(HttpStatusCode.Created, new { eventinfo = addedEvent, attendees = JsonUser.FromDatabase(eventAttendees) })); } return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Unable to create event")); }