示例#1
0
文件: Create.cs 项目: agrc/wri-webapi
        public static async Task<MessageWithStatus> Actions(IDbConnection connection, IQuery queries, int id, FeatureActions[] actions, string table)
        {
            if (actions == null)
            {
                return await Task.Factory.StartNew(() => new MessageWithStatus()
                {
                    Successful = true
                });
            }

            table = table.ToUpper();

            if (table != "POLY")
            {
                return await Task.Factory.StartNew(() => new MessageWithStatus()
                {
                    Successful = true
                });
            }

            foreach (var polyAction in actions)
            {
                // insert top level action
                var actionIds = await queries.ActionQueryAsync(connection, new
                {
                    id,
                    action = polyAction.Action
                });

                var actionId = actionIds.FirstOrDefault();

                if (!actionId.HasValue)
                {
                    return await Task.Factory.StartNew(() => new MessageWithStatus()
                    {
                        Successful = false,
                        Status = HttpStatusCode.InternalServerError,
                        Message = "Problem getting scope identity from actions insert."
                    });
                }

                // insert second level treatment
                foreach (var treatment in polyAction.Treatments)
                {
                    var treatmentIds = await queries.TreatmentQueryAsync(connection, new
                    {
                        id = actionId,
                        treatment = treatment.Treatment
                    });

                    var treatmentId = treatmentIds.FirstOrDefault();

                    if (!treatmentId.HasValue)
                    {
                        return await Task.Factory.StartNew(() => new MessageWithStatus()
                        {
                            Successful = false,
                            Status = HttpStatusCode.InternalServerError,
                            Message = "Problem getting scope identity from treatment insert."
                        });
                    }

                    // move on if no herbicides
                    if (treatment.Herbicides == null)
                    {
                        continue;
                    }

                    // insert third level herbicide
                    foreach (var herbicide in treatment.Herbicides)
                    {
                        await queries.ExecuteAsync(connection, "Herbicide", new
                        {
                            id = treatmentId,
                            herbicide
                        });
                    }
                }
            }

            return await Task.Factory.StartNew(() => new MessageWithStatus()
            {
                Successful = true
            });
        }