public async Task <IActionResult> GetOrganizationData([FromRoute] string name, [FromRoute] double latitude, [FromRoute] double longitude) { try { var organization = await _organizationStore.GetOrganizationAsync(name, latitude, longitude); if (organization == null) { var organizationYandexInfo = await _yandexSearchService.SearchOrganization(name); organization = await _organizationStore.CreateOrganizationAsync(name, latitude, longitude, organizationYandexInfo); if (organizationYandexInfo?.Links != null) { foreach (var link in organizationYandexInfo.Links) { var socialNetworkType = link.GetSocialNetworkType(); if ((int)socialNetworkType >= 1 || (int)socialNetworkType <= 3) { await _organizationStore.AddSocialNetworkAccountAsync(organization.Id, socialNetworkType, link.Href); } } } } var socialNeгtworkAccounts = await _organizationStore.GetOrganizationSocialNetworkAccountsAsync(organization.Id); var ratings = (await _organizationStore.GetRatingByOrganization(organization.Id))?.ToList(); var result = new OrganizationInfo { Id = organization.Id, Name = organization.Name, RuName = organization.RuName, Address = organization.Address, Latitude = organization.Latitude, Longitude = organization.Longitude, Phone = organization.Phone, CountOfFreePlacement = OrganizationHelper.GetCountEmptyPlacement(organization.CountOfAvailablePlacement ?? 15), CountOfAvailablePlacement = organization.CountOfAvailablePlacement ?? 15, Description = organization.Description, WorkingTimes = new WorkingTimes { From = $"{organization.WorkingStartHour}:{organization.WorkingStartMinute}", To = $"{organization.WorkingEndHour}:{organization.WorkingEndMinute}" }, SocialNeгtworkAccounts = socialNeгtworkAccounts.Select(acc => new OrganizationSocialNetwork { Type = acc.Type, Url = acc.Url }), Rating = organization.Rating ?? 0, UserRatings = ratings.Select(rating => new UserRating { UserId = rating.UserId, Name = rating.Name, Surname = rating.Surname, Phone = rating.Phone, Rate = rating.Rating, Text = rating.Text }), Categories = organization.Categories, Url = organization.Url, WorkingTimeStr = organization.WorkingTimeStr }; return(Ok(result)); } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError)); } }
public async Task <IActionResult> Rate([FromBody] RateVM rateVM) { try { if (!rateVM.OrganizationId.HasValue || !rateVM.Rate.HasValue || !rateVM.UserId.HasValue) { return(BadRequest()); } var user = await _userStore.GetUserAsync(rateVM.UserId.Value); if (user == null) { return(BadRequest()); } var organization = await _organizationStore.GetOrganizationAsync(rateVM.OrganizationId.Value); if (organization == null) { return(BadRequest()); } await _organizationStore.Rate(new RatingEntity { OrganizationId = rateVM.OrganizationId.Value, UserId = rateVM.UserId.Value, Rating = rateVM.Rate.Value, Text = rateVM.Text }); var ratings = (await _organizationStore.GetRatingByOrganization(rateVM.OrganizationId.Value))?.ToList(); var averageRating = ratings.Average(rating => rating.Rating); await _organizationStore.UpdateRatingAsync(rateVM.OrganizationId.Value, averageRating); var socialNeгtworkAccounts = await _organizationStore.GetOrganizationSocialNetworkAccountsAsync(organization.Id); var result = new OrganizationInfo { Id = organization.Id, Name = organization.Name, RuName = organization.RuName, Address = organization.Address, Latitude = organization.Latitude, Longitude = organization.Longitude, Description = organization.Description, Phone = organization.Phone, Rating = averageRating, CountOfAvailablePlacement = organization.CountOfAvailablePlacement ?? 15, UserRatings = ratings.Select(rating => new UserRating { UserId = rating.UserId, Name = rating.Name, Surname = rating.Surname, Phone = rating.Phone, Rate = rating.Rating, Text = rating.Text }), WorkingTimes = new WorkingTimes { From = $"{organization.WorkingStartHour}:{organization.WorkingStartMinute}", To = $"{organization.WorkingEndHour}:{organization.WorkingEndMinute}" }, SocialNeгtworkAccounts = socialNeгtworkAccounts.Select(acc => new OrganizationSocialNetwork { Type = acc.Type, Url = acc.Url }), CountOfFreePlacement = OrganizationHelper.GetCountEmptyPlacement(organization.CountOfAvailablePlacement ?? 15), Categories = organization.Categories, Url = organization.Url, WorkingTimeStr = organization.WorkingTimeStr }; return(Ok(result)); } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError)); } }