Example #1
0
 public IHttpActionResult PutOrganization(int id, OrganizationEditDTO organization)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (id != organization.Id)
     {
         return(BadRequest());
     }
     try
     {
         Organization org        = organization.FromDTO();
         UnitOfWork   unitOfWork = new UnitOfWork(factory);
         unitOfWork.OrganizationsRepository.Update(org);
         unitOfWork.Save();
         OrganizationEditDTO dto = unitOfWork.OrganizationsRepository
                                   .Get(d => d.Id == id, includeProperties: "Country,OrganizationLegalForm")
                                   .FirstOrDefault().MapToEdit();
         return(Ok(dto));
     }
     catch (NotFoundException nfe)
     {
         return(NotFound());
     }
     catch (ConflictException ce)
     {
         return(Conflict());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Example #2
0
        public IHttpActionResult PostOrganization(OrganizationEditDTO organization)
        {
            try
            {
                if (identityHelper.GetUserTableName(User) == "Employee")
                {
                    if (organization.CountryId == null || organization.CountryId == 0)
                    {
                        return(BadRequest("Empty CountryId"));
                    }

                    Organization org = organization.FromDTO();
                    //now this information is stored in OrganizationOwner (we do not need this anymore)
                    //  organization.EmployeeId = identityHelper.GetUserTableId(User);
                    UnitOfWork unitOfWork = new UnitOfWork(factory);
                    org.Id = org.NewId(unitOfWork);
                    unitOfWork.OrganizationsRepository.Insert(org);

                    unitOfWork.OrganizationOwnersRepository.Insert(
                        new OrganizationOwner()
                    {
                        DateBegin      = DateTime.Now,
                        EmployeeId     = identityHelper.GetUserTableId(User),
                        OrganizationId = org.Id
                    });

                    //constant types
                    List <OrganizationPropertyType> types = unitOfWork.OrganizationPropertyTypesRepository.Get(d => d.Constant == true && d.CountryId == org.CountryId).ToList();
                    foreach (var type in types)
                    {
                        OrganizationProperty property = new OrganizationProperty();
                        property.Id = property.NewId(unitOfWork);
                        property.OrganizationPropertyTypeId = type.Id;
                        property.OrganizationId             = org.Id;
                        unitOfWork.OrganizationPropertiesRepository.Insert(property);
                    }
                    unitOfWork.Save();
                    OrganizationEditDTO dto = unitOfWork.OrganizationsRepository
                                              .Get(d => d.Id == org.Id, includeProperties: "Country,OrganizationLegalForm")
                                              .FirstOrDefault().MapToEdit();

                    return(CreatedAtRoute("GetOrganizationEdit", new { id = dto.Id }, dto));
                }
            }
            catch (NotFoundException nfe)
            {
                return(NotFound());
            }
            catch (ConflictException ce)
            {
                return(Conflict());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
            return(BadRequest());
        }