Created
January 24, 2025 10:45
-
-
Save dilshod1d/13123aa4ce7dd6aeea364722f71f95ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yourappname; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.servlet.config.annotation.CorsRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
@SpringBootApplication | |
public class YourAppNameApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(YourAppNameApplication.class, args); | |
} | |
} | |
@Configuration | |
class WebConfig { | |
@Bean | |
public WebMvcConfigurer corsConfigurer() { | |
return new WebMvcConfigurer() { | |
@Override | |
public void addCorsMappings(CorsRegistry registry) { | |
registry.addMapping("/**").allowedOrigins("*"); | |
} | |
}; | |
} | |
} | |
// --- Models --- | |
package com.yourappname.model; | |
import jakarta.persistence.*; | |
import lombok.Data; | |
import java.time.LocalDate; | |
@Entity | |
@Data | |
public class Subscription { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
private Long planId; | |
private LocalDate startDate; | |
private LocalDate endDate; | |
@Enumerated(EnumType.STRING) | |
private Duration duration; | |
@Enumerated(EnumType.STRING) | |
private Status status; | |
private String paymentMethod; | |
private Double totalPrice; | |
private Boolean isTrial; | |
public enum Duration { | |
MONTHLY, THREE_MONTHS, SIX_MONTHS, ANNUAL | |
} | |
public enum Status { | |
ACTIVE, PENDING, CANCELED, EXPIRED | |
} | |
} | |
@Entity | |
@Data | |
public class Plan { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
private String name; | |
private Double monthlyPrice; | |
private Double threeMonthPrice; | |
private Double sixMonthPrice; | |
private Double annualPrice; | |
private Integer maxStaffs; | |
} | |
// --- Repositories --- | |
package com.yourappname.repository; | |
import com.yourappname.model.Subscription; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
public interface SubscriptionRepository extends JpaRepository<Subscription, Long> { | |
} | |
import com.yourappname.model.Plan; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
public interface PlanRepository extends JpaRepository<Plan, Long> { | |
} | |
// --- Services --- | |
package com.yourappname.service; | |
import com.yourappname.model.Plan; | |
import com.yourappname.model.Subscription; | |
import com.yourappname.repository.PlanRepository; | |
import com.yourappname.repository.SubscriptionRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
import java.util.Optional; | |
@Service | |
public class SubscriptionService { | |
@Autowired | |
private SubscriptionRepository subscriptionRepository; | |
@Autowired | |
private PlanRepository planRepository; | |
public List<Subscription> getAllSubscriptions() { | |
return subscriptionRepository.findAll(); | |
} | |
public Optional<Subscription> getSubscriptionById(Long id) { | |
return subscriptionRepository.findById(id); | |
} | |
public Subscription saveSubscription(Subscription subscription) { | |
return subscriptionRepository.save(subscription); | |
} | |
public void deleteSubscription(Long id) { | |
subscriptionRepository.deleteById(id); | |
} | |
public List<Plan> getAllPlans() { | |
return planRepository.findAll(); | |
} | |
public Optional<Plan> getPlanById(Long id) { | |
return planRepository.findById(id); | |
} | |
} | |
// --- Controllers --- | |
package com.yourappname.controller; | |
import com.yourappname.model.Plan; | |
import com.yourappname.model.Subscription; | |
import com.yourappname.service.SubscriptionService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.*; | |
import java.util.List; | |
import java.util.Optional; | |
@RestController | |
@RequestMapping("/api/subscriptions") | |
public class SubscriptionController { | |
@Autowired | |
private SubscriptionService subscriptionService; | |
@GetMapping | |
public List<Subscription> getAllSubscriptions() { | |
return subscriptionService.getAllSubscriptions(); | |
} | |
@GetMapping("/{id}") | |
public ResponseEntity<Subscription> getSubscriptionById(@PathVariable Long id) { | |
Optional<Subscription> subscription = subscriptionService.getSubscriptionById(id); | |
return subscription.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); | |
} | |
@PostMapping | |
public Subscription createSubscription(@RequestBody Subscription subscription) { | |
return subscriptionService.saveSubscription(subscription); | |
} | |
@DeleteMapping("/{id}") | |
public ResponseEntity<Void> deleteSubscription(@PathVariable Long id) { | |
subscriptionService.deleteSubscription(id); | |
return ResponseEntity.noContent().build(); | |
} | |
@GetMapping("/plans") | |
public List<Plan> getAllPlans() { | |
return subscriptionService.getAllPlans(); | |
} | |
@GetMapping("/plans/{id}") | |
public ResponseEntity<Plan> getPlanById(@PathVariable Long id) { | |
Optional<Plan> plan = subscriptionService.getPlanById(id); | |
return plan.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); | |
} | |
} | |
// --- Security Configuration --- | |
package com.yourappname.config; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.web.SecurityFilterChain; | |
@Configuration | |
public class SecurityConfig { | |
@Bean | |
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | |
http.csrf().disable() | |
.authorizeRequests() | |
.antMatchers("/api/**").permitAll() | |
.anyRequest().authenticated(); | |
return http.build(); | |
} | |
} | |
// --- Utility Configuration --- | |
package com.yourappname.config; | |
import lombok.Getter; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@Getter | |
public class PlanConfig { | |
public enum Plan { | |
BASIC("Basic", 150, 1, 150, false, 0, 0, 0), | |
STANDARD("Standard", 300, 3, 300, true, 3, 90, 25000), | |
PREMIUM("Premium", 500, 5, 500, true, 5, 180, 38000); | |
private final String name; | |
private final int smsLimit; | |
private final int maxStaffs; | |
private final int monthlyPatientCount; | |
private final boolean diagnosticsIncluded; | |
private final int technicianAddingCount; | |
private final int monthlyOrderCount; | |
private final int monthlyPrice; | |
Plan(String name, int smsLimit, int maxStaffs, int monthlyPatientCount, boolean diagnosticsIncluded, int technicianAddingCount, int monthlyOrderCount, int monthlyPrice) { | |
this.name = name; | |
this.smsLimit = smsLimit; | |
this.maxStaffs = maxStaffs; | |
this.monthlyPatientCount = monthlyPatientCount; | |
this.diagnosticsIncluded = diagnosticsIncluded; | |
this.technicianAddingCount = technicianAddingCount; | |
this.monthlyOrderCount = monthlyOrderCount; | |
this.monthlyPrice = monthlyPrice; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment