Пример #1
0
        public async Task <ActionResult> CreateCompany([FromBody] PredlaganjeSaradnjeIRC.Data.Model.Company company,
                                                       [FromHeader] string Authorization)
        {
            var header = new Metadata();

            header.Add("Authorization", $"Bearer {Authorization}");
            var option = new CallOptions(header);

            var enu           = company.Contacts.LastOrDefault()?.ContactType ?? 0;
            var createCompany = await httpsCompanyClient.AddCompanyAsync(new CompanyRequest
            {
                CompanyId = 0,
                Location  = new LocationRequest
                {
                    Door       = company.Locations.LastOrDefault()?.Door ?? 0,
                    Number     = company.Locations.LastOrDefault()?.Number ?? 0,
                    Storey     = company.Locations.LastOrDefault()?.Storey ?? 0,
                    StreetName = company.Locations.LastOrDefault().StreetName ?? "",
                    CityId     = company.Locations.LastOrDefault()?.City?.Id ?? 0
                },
                Name     = company.Name,
                Password = company.Password,
                Username = company.Username,
                Contact  = new ContactRequest
                {
                    ContactType = (PredlaganjeSaradnjeIRCDemo.GRPCService.ContactType)enu,
                    Content     = company.Contacts.LastOrDefault()?.Content ?? "empty"
                }
            }, option);

            if (createCompany.Status == GRPCService.StatusCode.Error)
            {
                return(BadRequest());
            }
            else
            {
                var fullCompany = new CompanyView
                {
                    Id       = createCompany.Company.Id,
                    Name     = createCompany.Company.Name,
                    Username = createCompany.Company.Username,
                    Password = createCompany.Company.Password,
                    Contact  = new PredlaganjeSaradnjeIRC.Data.Model.Contact
                    {
                        Id          = createCompany.Contact.Id,
                        Content     = createCompany.Contact.Content,
                        ContactType = (PredlaganjeSaradnjeIRC.Data.Model.ContactType)createCompany.Contact.ContactType
                    }

                    ,
                    Location = new PredlaganjeSaradnjeIRC.Data.Model.Location
                    {
                        Id   = createCompany.Location.Id,
                        City = new PredlaganjeSaradnjeIRC.Data.Model.City
                        {
                            Id         = createCompany.Location.City.Id,
                            Name       = createCompany.Location.City.Name,
                            PostalCode = createCompany.Location.City.Postalcode,
                        },
                        Door       = createCompany.Location.Door,
                        Number     = createCompany.Location.Number,
                        Storey     = createCompany.Location.Storey,
                        StreetName = createCompany.Location.StreetName,
                    }
                };

                return(Ok(fullCompany));
            }
        }
Пример #2
0
        public async Task <ActionResult> UpdateCompany(int id, [FromBody] PredlaganjeSaradnjeIRC.Data.Model.Company updatedCompany,
                                                       [FromHeader] string Authorization)
        {
            var header = new Metadata();

            header.Add("Authorization", $"Bearer {Authorization}");
            var option = new CallOptions(header);

            var updated = await httpsCompanyClient.UpdateCompanyAsync(new CompanyRequest
            {
                CompanyId = id,
                Name      = updatedCompany.Name,
                Password  = updatedCompany.Password,
                Username  = updatedCompany.Username,
                Contact   = new ContactRequest
                {
                    ContactType = (PredlaganjeSaradnjeIRCDemo.GRPCService.ContactType)updatedCompany.Contacts.LastOrDefault()?.ContactType,
                    Content     = updatedCompany.Contacts.LastOrDefault()?.Content
                },
                Location = new LocationRequest
                {
                    Door       = updatedCompany.Locations.LastOrDefault()?.Door ?? 0,
                    Number     = updatedCompany.Locations.LastOrDefault()?.Number ?? 0,
                    Storey     = updatedCompany.Locations.LastOrDefault()?.Storey ?? 0,
                    StreetName = updatedCompany.Locations.LastOrDefault()?.StreetName,
                    CityId     = updatedCompany.Locations.LastOrDefault()?.City?.Id ?? 0
                },
            }, option);

            if (updated.Status == GRPCService.StatusCode.Ok)
            {
                var gotValues = new CompanyView
                {
                    Id       = updated.Company.Id,
                    Name     = updated.Company.Name,
                    Username = updated.Company.Username,
                    Password = updated.Company.Password,
                    Contact  = new PredlaganjeSaradnjeIRC.Data.Model.Contact
                    {
                        Id          = updated.Contact.Id,
                        Content     = updated.Contact.Content,
                        ContactType = (PredlaganjeSaradnjeIRC.Data.Model.ContactType)updated.Contact.ContactType
                    }

                    ,
                    Location = new PredlaganjeSaradnjeIRC.Data.Model.Location
                    {
                        Id   = updated.Location.Id,
                        City = new PredlaganjeSaradnjeIRC.Data.Model.City
                        {
                            Id         = updated.Location.City.Id,
                            Name       = updated.Location.City.Name,
                            PostalCode = updated.Location.City.Postalcode,
                        },
                        Door       = updated.Location.Door,
                        Number     = updated.Location.Number,
                        Storey     = updated.Location.Storey,
                        StreetName = updated.Location.StreetName,
                    }
                };

                if (gotValues == null)
                {
                    return(BadRequest());
                }
                return(Ok(gotValues));
            }
            return(Forbid("Nije moguce izmeniti kompaniju!"));
        }