public async Task <ActionResult <CompanyDto> > CreateCompanyWithBankruptTime(CompanyAddWithBankruptTimeDto company)
        {
            //在3.0之前需要这样写
            //现在不需要了,因为当Controller使用[ApiController]属性进行注解的时候,如果遇到验证错误
            //那么就会自动返回400错误

            //if (!ModelState.IsValid)
            //{
            //    return UnprocessableEntity(ModelState);
            //}

            var entity = _mapper.Map <Company>(company);

            _companyRepository.AddCompany(entity);
            await _companyRepository.SaveAsync();

            var returnDto = _mapper.Map <CompanyDto>(entity);

            var links = CreateLinksForCompany(returnDto.Id, null);

            var linkedDict = returnDto.ShapeData(null) as IDictionary <string, object>;

            linkedDict.Add("links", links);

            return(CreatedAtRoute(nameof(GetCompany), new { companyId = linkedDict["Id"] }, linkedDict));
        }
        public async Task <ActionResult <CompanyDto> > CreateCompanyWithBankruptTime(CompanyAddWithBankruptTimeDto company)
        {
            var entity = _mapper.Map <Company>(company);

            _companyRepository.AddCompany(entity);
            await _companyRepository.SaveAsync();

            var returnDto  = _mapper.Map <CompanyDto>(entity);
            var links      = CreateLinksForCompany(returnDto.Id, null);
            var linkedDict = returnDto.ShapeData(null) as IDictionary <string, object>;

            linkedDict.Add("links", links);
            return(CreatedAtRoute(nameof(GetCompany), new { companyId = linkedDict["Id"] }, linkedDict));
        }
Example #3
0
        public async Task <IActionResult> CreateCompanyWithBankruptTime([FromBody] CompanyAddWithBankruptTimeDto company)
        {
            //这段代码现在可以去掉,最早版本Asp.Net Core不行
            //if (company == null)
            //{
            //    return BadRequest();
            //}

            var entity = _mapper.Map <Company>(company);

            _companyRepository.AddCompany(entity);
            var result = await _companyRepository.SaveAsync();

            var returnDto = _mapper.Map <CompanyDto>(entity);

            var links      = CreateLinksForCompany(returnDto.Id, null);
            var linkedDict = returnDto.ShapedData(null)
                             as IDictionary <string, object>;

            linkedDict.Add("links", links);
            return(CreatedAtRoute(nameof(GetCompany), new { companyId = linkedDict["Id"] }, linkedDict));
        }
Example #4
0
        public async Task <IActionResult> CreateCompanyWithBankruptTime([FromBody] CompanyAddWithBankruptTimeDto companyWithBankruptTime,
                                                                        [FromHeader(Name = "Accept")]
                                                                        string acceptMediaType)
        {
            //尝试解析 MediaTypeHeaderValue(视频P43)
            if (!MediaTypeHeaderValue.TryParse(acceptMediaType, out MediaTypeHeaderValue parsedAcceptMediaType))
            {
                return(BadRequest());
            }

            //是否需要 links(HATEOAS)(视频P41-43)
            bool includeLinks = parsedAcceptMediaType.SubTypeWithoutSuffix
                                .EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase); //大小写不敏感
            //是否需要 Full Dto
            bool isFull = parsedAcceptMediaType.SubTypeWithoutSuffix
                          .ToString()
                          .Contains("full", StringComparison.InvariantCultureIgnoreCase);

            var entity = _mapper.Map <Company>(companyWithBankruptTime);

            _companyRepository.AddCompany(entity);
            await _companyRepository.SaveAsync();

            var shapedData = isFull ?
                             _mapper.Map <CompanyFullDto>(entity).ShapeData(null)
                             :
                             _mapper.Map <CompanyFriendlyDto>(entity).ShapeData(null);

            if (includeLinks)
            {
                var companyDict = shapedData as IDictionary <string, object>;
                var links       = CreateLinksForCompany(entity.Id, null);
                companyDict.Add("links", links);
                return(CreatedAtRoute(nameof(GetCompany), new { companyId = entity.Id }, companyDict));
            }

            return(CreatedAtRoute(nameof(GetCompany), new { companyId = entity.Id }, shapedData));
        }
Example #5
0
        public async Task <ActionResult <CompanyDto> > CreateCompanyAddWithBankruptTime([FromBody] CompanyAddWithBankruptTimeDto company)
        {
            // 使用了api controller就不需要这个判断了。
            //if (company == null)
            //{
            //    return BadRequest();
            //}


            var entity = _mapper.Map <Company>(company);

            _companyRepositroy.AddCompany(entity);
            await _companyRepositroy.SaveAsync();

            var returnDto = _mapper.Map <CompanyDto>(entity);


            var links = CreateLinksForCompany(returnDto.Id, null);

            var linkedDict = returnDto.shapeData(null) as IDictionary <string, object>;

            linkedDict.Add("links", links);

            return(CreatedAtRoute(nameof(GetCompany), new { companyId = linkedDict["Id"] }, linkedDict));
        }