Exemple #1
0
        public async Task <Employee.PutResponse> Put([FromBody] Employee.PutRequest body)
        {
            var ipAddress   = HttpContext.Connection.RemoteIpAddress.ToString();
            var returnValue = new Employee.PutResponse();
            // Validation
            var errors = await Employee.PutRequest.Validation(body);

            if (errors.Count() != 0)
            {
                returnValue.ValidationErrors = errors;
                _logger.LogInformation($"Sended with code {Response.StatusCode} and validation errors to {ipAddress}");
                return(returnValue);
            }
            // Database query
            var result = await Queries.PutEmployee(body);

            if (result.Item2 != null)
            {
                Response.StatusCode = result.Item2.GetValueOrDefault();
                _logger.LogInformation($"Sended with code {Response.StatusCode} to {ipAddress}");
                return(null);
            }
            else
            {
                returnValue = result.Item1;
                _logger.LogInformation("Put: Inserted in database.");
            }
            // Finishing
            _logger.LogInformation($"Sended with code {Response.StatusCode} to {ipAddress}");
            return(returnValue);
        }
Exemple #2
0
        public static async Task <(Employee.PutResponse, int?/* Error code */)> PutEmployee(Employee.PutRequest data)
        {
            var returnValue = new Employee.PutResponse();

            using (var connection = new SqlConnection(ConnectionStrings.Default))
            {
                // Opening connection
                connection.Open();
                // Configuring command
                var command = new SqlCommand(@"
                    begin transaction;

                    insert into [Employees](
                        [Name], [Surname], [Patronymic], [Department], [Position], [Supervisor]
                    )
                    values(
                        @Name, @Surname, @Patronymic, @Department, @Position, @Supervisor
                    );

                    select top (1) [Id] from [Employees]
                        order by [Id] desc;

                    commit;
                ", connection);
                // Filling parameters
                command.Parameters.AddWithValue("@Name", data.Name);
                command.Parameters.AddWithValue("@Surname", data.Surname);
                command.Parameters.AddWithValue("@Patronymic", (object)data.Patronymic ?? DBNull.Value);
                command.Parameters.AddWithValue("@Department", data.Department);
                command.Parameters.AddWithValue("@Position", data.Position);
                command.Parameters.AddWithValue("@Supervisor", (object)data.Supervisor ?? DBNull.Value);
                // Executing
                var reader = await command.ExecuteReaderAsync();

                // Reading query result
                if (reader.Read())
                {
                    returnValue.Result = reader.GetInt32(0);
                }
                reader.Close();
                // Closing connection
                connection.Close();
            }
            return(returnValue, null);
        }