コード例 #1
0
        public async Task <ActionResult <PersonName> > Post(string name, string malemidname = null, string femalemidname = null)
        {
            if (!string.IsNullOrWhiteSpace(name) && _ctx.PersonNameExists(name))
            {
                return(Conflict());
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(name))
                {
                    var gender = string.IsNullOrWhiteSpace(malemidname) || string.IsNullOrWhiteSpace(femalemidname) ? Gender.F : Gender.M;

                    var item = new PersonName
                    {
                        Name   = name,
                        Gender = gender,
                        Part   = NamePart.FIRST
                    };

                    if (gender == Gender.M && !string.IsNullOrWhiteSpace(malemidname) && !string.IsNullOrWhiteSpace(femalemidname))
                    {
                        item.Derived.Add(new PersonName {
                            Name = malemidname, Gender = Gender.M, Part = NamePart.MID, BaseName = item
                        });
                        item.Derived.Add(new PersonName {
                            Name = femalemidname, Gender = Gender.F, Part = NamePart.MID, BaseName = item
                        });
                    }
                    ;

                    _ctx.PersonNames.Add(item);
                    await _ctx.SaveChangesAsync();

                    return(CreatedAtAction("Get", new { Id = item.Id }, item));
                }
                else
                {
                    return(BadRequest());
                };
            };
        }