public IActionResult Put(string id, [FromBody] object content)
        {
            SqlClient    sqlClient    = new SqlClient();
            SearchClient searchClient = new SearchClient(
                Environment.GetEnvironmentVariable(ServiceConfiguration.AlgoliaAppId),
                Environment.GetEnvironmentVariable(ServiceConfiguration.AdminKey));
            SearchIndex index = searchClient.InitIndex(ServiceConfiguration.SearchIndex);

            if (Get(id) == null)
            {
                return(NotFound());
            }

            CompetitiveEvent updatedEvent = JsonConvert.DeserializeObject <CompetitiveEvent>(content.ToString());
            CompetitiveEvent currentEvent = sqlClient.GetCompetitiveEvent(
                SqlCommands.GetCompetitiveEvent,
                id);

            currentEvent = SqlOperations.UpdateCompetitiveEvent(currentEvent, updatedEvent);

            sqlClient.CreateOrInsert(
                SqlCommands.UpdateCompetitiveEvent,
                CompetitiveEvent.ToDictionary(currentEvent));
            index.PartialUpdateObject(
                currentEvent,
                createIfNotExists: false);

            return(Ok());
        }
        public IActionResult Post([FromBody] object content)
        {
            SqlClient    sqlClient    = new SqlClient();
            SearchClient searchClient = new SearchClient(
                Environment.GetEnvironmentVariable(ServiceConfiguration.AlgoliaAppId),
                Environment.GetEnvironmentVariable(ServiceConfiguration.AdminKey));
            SearchIndex index = searchClient.InitIndex(ServiceConfiguration.SearchIndex);

            CompetitiveEvent competitiveEvent = JsonConvert.DeserializeObject <CompetitiveEvent>(content.ToString());

            competitiveEvent.CreatedTimestamp = competitiveEvent.ModifiedTimestamp = DateTime.Now;
            competitiveEvent.Id = competitiveEvent.ObjectID = Guid.NewGuid().ToString();

            if (Get(competitiveEvent.Id) != null)
            {
                competitiveEvent.Id = competitiveEvent.ObjectID = Guid.NewGuid().ToString();
            }
            competitiveEvent.Version = 1;

            sqlClient.CreateOrInsert(
                SqlCommands.InsertCompetitiveEvent,
                CompetitiveEvent.ToDictionary(competitiveEvent));
            index.SaveObject(
                competitiveEvent,
                autoGenerateObjectId: false);

            return(Ok());
        }
        public IActionResult Get(string id)
        {
            SqlClient sqlClient = new SqlClient();

            CompetitiveEvent competitiveEvent = sqlClient.GetCompetitiveEvent(
                SqlCommands.GetCompetitiveEvent,
                id);

            if (competitiveEvent == null)
            {
                return(NotFound());
            }

            return(Ok(JsonConvert.SerializeObject(competitiveEvent)));
        }
        public static CompetitiveEvent GetCompetitiveEvent(
            this SqlClient client,
            string sqlCommand,
            string id)
        {
            if (String.IsNullOrEmpty(sqlCommand))
            {
                throw new ArgumentNullException(nameof(sqlCommand));
            }
            if (String.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            SqlConnection    connection       = null;
            CompetitiveEvent competitiveEvent = null;

            try
            {
                using (connection = new SqlConnection(client.ConnectionString))
                {
                    connection.Open();

                    var command = new SqlCommand(sqlCommand, connection);
                    command.Parameters.AddWithValue(SqlConstants.IdParameterName, id);

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            competitiveEvent = ReaderToCompetitiveEvent(reader);
                        }
                    }
                }

                return(competitiveEvent);
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                connection.Close();
            }
        }
        public static CompetitiveEvent UpdateCompetitiveEvent(CompetitiveEvent currentEvent, CompetitiveEvent updatedEvent)
        {
            if (currentEvent == null)
            {
                throw new ArgumentNullException(nameof(currentEvent));
            }
            if (updatedEvent == null)
            {
                throw new ArgumentNullException(nameof(updatedEvent));
            }

            currentEvent.Active = updatedEvent.Active;

            if (updatedEvent.Attachments != null)
            {
                currentEvent.Attachments = updatedEvent.Attachments;
            }
            if (updatedEvent.Alert != null)
            {
                currentEvent.Alert = updatedEvent.Alert;
            }

            currentEvent.Archive = updatedEvent.Archive;

            if (updatedEvent.Challenge != null)
            {
                currentEvent.Challenge = updatedEvent.Challenge;
            }
            if (updatedEvent.Contact != null)
            {
                currentEvent.Contact = updatedEvent.Contact;
            }
            if (updatedEvent.Description != null)
            {
                currentEvent.Description = updatedEvent.Description;
            }
            if (updatedEvent.Details != null)
            {
                currentEvent.Details = updatedEvent.Details;
            }

            currentEvent.Individual        = updatedEvent.Individual;
            currentEvent.HighSchool        = updatedEvent.HighSchool;
            currentEvent.MiddleSchool      = updatedEvent.MiddleSchool;
            currentEvent.ModifiedTimestamp = DateTime.Now;

            if (updatedEvent.Name != null)
            {
                currentEvent.Name = updatedEvent.Name;
            }
            if (updatedEvent.Overview != null)
            {
                currentEvent.Overview = updatedEvent.Overview;
            }
            if (updatedEvent.RelatedCareers != null)
            {
                currentEvent.RelatedCareers = updatedEvent.RelatedCareers;
            }
            if (updatedEvent.Rubric != null)
            {
                currentEvent.Rubric = updatedEvent.Rules;
            }
            if (updatedEvent.Rules != null)
            {
                currentEvent.Rules = updatedEvent.Rules;
            }

            currentEvent.ShowAlert = updatedEvent.ShowAlert;

            if (updatedEvent.StemIntegration != null)
            {
                currentEvent.StemIntegration = updatedEvent.StemIntegration;
            }
            if (updatedEvent.SubmittableUri != null)
            {
                currentEvent.SubmittableUri = updatedEvent.SubmittableUri;
            }

            currentEvent.Team = updatedEvent.Team;

            if (updatedEvent.Type != null)
            {
                currentEvent.Type = updatedEvent.Type;
            }

            currentEvent.Version += 1;

            return(currentEvent);
        }