コード例 #1
0
        public async Task <IActionResult> Update(string orgName, [FromBody] OrganizationForCreationDTO orgForUpdate)
        {
            if (HttpContext.Items.ContainsKey("organization"))
            {
                var oldOrg = HttpContext.Items["organization"] as Organization;

                if (oldOrg.Id == orgForUpdate.Id)

                {
                    var newOrg = this.mapper.Map <Organization>(orgForUpdate);
                    if (oldOrg.NormalizedName == this.normalizer.Normalize(orgName))
                    {
                        this.orgRepo.UpdateWithoutName(oldOrg, newOrg);
                        if (await this.orgRepo.SaveChangesAsync())
                        {
                            return(Ok(new { message = $"@{orgName} organization has been updated successfully" }));
                        }
                        else
                        {
                            return(BadRequest("Organization can't be modified"));
                        }
                    }
                    await this.orgRepo.UpdateWithNameAsync(oldOrg, newOrg);

                    if (await this.orgRepo.SaveChangesAsync())
                    {
                        return(Ok(new { message = $"@{orgName} organization has been updated successfully" }));
                    }
                    return(BadRequest("Organization can't be modified"));
                }
            }
            return(NotFound($"{orgName} organizaiton isn't exist"));
        }
コード例 #2
0
        public async Task <IActionResult> Create([FromBody] OrganizationForCreationDTO orgForCreateDTO)
        {
            var user   = HttpContext.Items["current-user"] as User;
            var newOrg = this.mapper.Map <Organization>(orgForCreateDTO);

            if (await this.orgRepo.CreateAsync(user, newOrg))
            {
                var orgToReturn = this.mapper.Map <OrganizationToReturnDTO>(newOrg);
                if (await this.orgRepo.SaveChangesAsync())
                {
                    return(CreatedAtRoute("GetOrganization",
                                          new { controller = "Organizations", orgName = newOrg.Name },
                                          new { message = $"@{newOrg.Name} organizaiton has been created successfully" }));
                }
                return(BadRequest($"Failed to create {orgForCreateDTO.Name} organization"));
            }
            return(BadRequest($"@{orgForCreateDTO.Name} organzization name is already taken"));
        }