API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using AutoMapper;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.Cors.Internal;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.IdentityModel.Tokens;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using UnivateProperties_API.Context;
  14. using UnivateProperties_API.Helpers;
  15. using UnivateProperties_API.Model.Banks;
  16. using UnivateProperties_API.Model.Campaigns;
  17. using UnivateProperties_API.Model.Communication;
  18. using UnivateProperties_API.Model.Financial;
  19. using UnivateProperties_API.Model.Misc;
  20. using UnivateProperties_API.Model.ProcessFlow;
  21. using UnivateProperties_API.Model.Properties;
  22. using UnivateProperties_API.Model.Region;
  23. using UnivateProperties_API.Model.Timeshare;
  24. using UnivateProperties_API.Model.Users;
  25. using UnivateProperties_API.Repository;
  26. using UnivateProperties_API.Repository.Banks;
  27. using UnivateProperties_API.Repository.Campaigns;
  28. using UnivateProperties_API.Repository.Communication;
  29. using UnivateProperties_API.Repository.Financial;
  30. using UnivateProperties_API.Repository.Logging;
  31. using UnivateProperties_API.Repository.Misc;
  32. using UnivateProperties_API.Repository.ProccessFlow;
  33. using UnivateProperties_API.Repository.Properties;
  34. using UnivateProperties_API.Repository.Region;
  35. using UnivateProperties_API.Repository.Timeshare;
  36. using UnivateProperties_API.Repository.Users;
  37. namespace UnivateProperties_API
  38. {
  39. public class Startup
  40. {
  41. public Startup(IConfiguration configuration)
  42. {
  43. Configuration = configuration;
  44. }
  45. public IConfiguration Configuration { get; }
  46. public void ConfigureServices(IServiceCollection services)
  47. {
  48. services.AddAutoMapper();
  49. services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  50. {
  51. builder.AllowAnyOrigin()
  52. .AllowAnyMethod()
  53. .AllowAnyHeader();
  54. }));
  55. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  56. services.AddDbContext<DataContext>(o => o.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
  57. MyCommon.TenderUrl = Configuration.GetConnectionString("TenderConnection");
  58. var appSettingsSection = Configuration.GetSection("AppSettings");
  59. services.Configure<AppSettings>(appSettingsSection);
  60. // configure jwt authentication
  61. var appSettings = appSettingsSection.Get<AppSettings>();
  62. var key = Encoding.ASCII.GetBytes(appSettings.Secret);
  63. services.AddAuthentication(x =>
  64. {
  65. x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  66. x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  67. })
  68. .AddJwtBearer(x =>
  69. {
  70. x.Events = new JwtBearerEvents
  71. {
  72. OnTokenValidated = context =>
  73. {
  74. var registerRepository = context.HttpContext.RequestServices.GetRequiredService<IRegisterRepository>();
  75. var userId = int.Parse(context.Principal.Identity.Name);
  76. var user = registerRepository.GetById(userId);
  77. if (user == null)
  78. {
  79. // return unauthorized if user no longer exists
  80. context.Fail("Unauthorized");
  81. }
  82. return Task.CompletedTask;
  83. }
  84. };
  85. x.RequireHttpsMetadata = false;
  86. x.SaveToken = true;
  87. x.TokenValidationParameters = new TokenValidationParameters
  88. {
  89. ValidateIssuerSigningKey = true,
  90. IssuerSigningKey = new SymmetricSecurityKey(key),
  91. ValidateIssuer = false,
  92. ValidateAudience = false
  93. };
  94. });
  95. #region ProcessFlow
  96. services.AddTransient<IBidRepository, BidRepository>();
  97. #endregion
  98. #region Property
  99. services.AddTransient<IRepository<Agent>, AgentRepository>();
  100. services.AddTransient<IRegisterRepository, RegisterRepository>();
  101. services.AddTransient<IRepository<Agency>, AgencyRepository>();
  102. services.AddTransient<IRepository<Email>, EmailRepository>();
  103. services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
  104. services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
  105. services.AddTransient<IPropertyRepository, PropertyRepository>();
  106. services.AddTransient<IPropertyImageRepository, PropertyImageRepository>();
  107. services.AddTransient<IRepository<PropertyType>, PropertyTypeRepository>();
  108. services.AddTransient<IRepository<PropertyUserField>, PropertyUserFieldRepository>();
  109. services.AddTransient<IRepository<UserDefinedField>, UserDefinedFieldRepository>();
  110. services.AddTransient<IUserDefinedGroupRepository, UserDefinedGroupRepository>();
  111. services.AddTransient<IRepository<Payment>, PaymentRepository>();
  112. #endregion Property
  113. #region Region
  114. services.AddTransient<IRepository<Province>, ProvinceRepository>();
  115. services.AddTransient<ICityRepository, CityRepository>();
  116. services.AddTransient<ISuburbRepository, SuburbRepository>();
  117. #endregion Region
  118. #region Timeshare
  119. services.AddTransient<IRepository<Status>, StatusRepository>();
  120. services.AddTransient<IRepository<Season>, SeasonRepository>();
  121. services.AddTransient<IRepository<UnitConfiguration>, UnitConfigurationRepository>();
  122. services.AddTransient<IRepository<TimeshareWeek>, WeekRepository>();
  123. services.AddTransient<IRepository<TimeshareWeek>, WeekRepository>();
  124. services.AddTransient<IRepository<Bank>, BankAccountRepository>();
  125. services.AddTransient<IRepository<BankAccount>, BankAccountRepository>();
  126. #endregion Timeshare
  127. #region User
  128. services.AddScoped<IRegisterRepository, RegisterRepository>();
  129. services.AddTransient<IRepository<Agent>, AgentRepository>();
  130. services.AddTransient<IRegisterRepository, RegisterRepository>();
  131. services.AddTransient<IRepository<Agency>, AgencyRepository>();
  132. services.AddTransient<IRepository<User>, UserRepository>();
  133. services.AddTransient<IRepository<Individual>, IndividualRepository>();
  134. #endregion User
  135. #region Communication
  136. services.AddTransient<IRepository<Template>, TemplateRepository>();
  137. services.AddTransient<IRepository<Email>, EmailRepository>();
  138. services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
  139. services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
  140. #endregion Communication
  141. #region Logs
  142. services.AddTransient<ISearchLogRepository, SearchLogRepository>();
  143. #endregion
  144. #region Misc
  145. services.AddTransient<ICarouselRepository, CarouselRepository>();
  146. services.AddTransient<IRepository<PlaceHolderFormat>, PlaceHolderFormatRepository>();
  147. #endregion
  148. #region Campaign
  149. services.AddTransient<ICampaignRepository, CampaignRepository>();
  150. #endregion
  151. services.Configure<MvcOptions>(options =>
  152. {
  153. options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  154. });
  155. }
  156. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  157. {
  158. UpdateDatabase(app);
  159. if (env.IsDevelopment())
  160. {
  161. app.UseDeveloperExceptionPage();
  162. }
  163. app.UseCors(x => x
  164. .AllowAnyOrigin()
  165. .AllowAnyMethod()
  166. .AllowAnyHeader());
  167. app.UseAuthentication();
  168. app.UseHttpsRedirection();
  169. app.UseMvc();
  170. }
  171. private static void UpdateDatabase(IApplicationBuilder app)
  172. {
  173. using (var serviceScope = app.ApplicationServices
  174. .GetRequiredService<IServiceScopeFactory>()
  175. .CreateScope())
  176. {
  177. using (var context = serviceScope.ServiceProvider.GetService<DataContext>())
  178. {
  179. //context.Database.Migrate();
  180. }
  181. }
  182. }
  183. }
  184. }