Untitled

mail@pastecode.io avatar
unknown
java
25 days ago
6.0 kB
0
Indexable
Never
package com.javatools.springbmongodbatlas.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Generated;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.annotation.JsonFormat;


import java.time.LocalDateTime;
import java.util.Date;

@Document(collection="members")
@Data
@AllArgsConstructor
@NoArgsConstructor

public class Member {
    @Id
    private String memberId;
    private String type_id;
    private String first_name;
    private String middle_name;
    private String last_name;
    private String address;
    private String address2;
    private String city;
    private String state;
    private String zip_code;
    private String phone_number;
    private String email_address;
    private Date date_of_birth;
    private String password;
    private String dl_number;
    private Date dl_issue_date;
    private Date dl_expiration_date;
    private String social_security_number;
    private String members_dl_photo;
    private String members_photo;
    private String members_signature;
    private String e_email_address;
    private String e_first_name;
    private String e_last_name;
    private String e_phone_number;
    private String e_contact_notes;
    private String prefer_id;
    private String is_active;
    private LocalDateTime created_at;
    private LocalDateTime updated_at;
    private LocalDateTime deleted_at;
}

spring.application.name=springbmongodbatlas
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=Member
#spring.data.mongodb.uri=mongodb+srv://mfk2512s:Welcome786$@mfk2512.kkbvieu.mongodb.net/?retryWrites=true&w=majority&appName=mfk2512
spring.data.mongodb.uri=mongodb+srv://cursorocity:Welcome786$@cluster0.jtrq8gj.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
server.port = 9090



package com.javatools.springbmongodbatlas.repository;


import com.javatools.springbmongodbatlas.model.Member;
import org.springframework.data.mongodb.repository.MongoRepository;


import java.util.List;

public interface MemberRepository extends MongoRepository <Member, String> {

    List<Member> findByCity(String city);
}

package com.javatools.springbmongodbatlas.service;

import java.text.SimpleDateFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

@Configuration
public class JacksonConfig {

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        converter.setObjectMapper(objectMapper);
        return converter;
    }
}




package com.javatools.springbmongodbatlas.controller;

import java.util.List;

import com.javatools.springbmongodbatlas.model.Member;
import com.javatools.springbmongodbatlas.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

@CrossOrigin("*")

@RestController
@RequestMapping("/members")
public class MemberController {

    @Autowired
    private MemberService service;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)

    public Member createMember(@RequestBody Member member) {

        return service.addMember(member);
    }

    @GetMapping
    public List<Member> getMember(){
        return service.findAllMembers();
    }

//    @GetMapping("{memberId}")
//    public Member getMember(@PathVariable Long Id) {
//        return service.getMemberById(Id);
//    }

//    @GetMapping("/severity/{severity}")
//    public List<Task> findTaskUsingSeverity(@PathVariable int severity){
//        return service.getTaskBySeverity(severity);
//
//    }
//
//    @GetMapping("/assignee/{assignee}")
//    public List<Task> getTaskByAssignee(@PathVariable String assignee){
//        return service.getTaskByAssignee(assignee);
//    }

//    @PutMapping
//    public Member modifyTask(@RequestBody Member member) {
//        return service.updateMember(member);
//    }

//    @DeleteMapping("/{memberId}")
//    public String deleteTask(@PathVariable String taskId) {
//        return service.deleteMember(memberId);
//    }

}



package com.javatools.springbmongodbatlas.service;

import com.javatools.springbmongodbatlas.model.Member;
import com.javatools.springbmongodbatlas.repository.MemberRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class MemberService {

    @Autowired
    private MemberRepository repository;

//    public Member addMember(Member member){
////        member.setMemberId(String.valueOf(UUID.randomUUID().toString().split("-")[0]));
//        member.setMemberId(new ObjectId().toString());
//        return repository.save(member);
//    }

    private int memberIdCounter = 100;

    public Member addMember(Member member){
        member.setMemberId(String.valueOf(memberIdCounter++)); // Increment counter and use as member ID
        return repository.save(member);
    }

    public List<Member> findAllMembers(){
        return repository.findAll();
    }

    public String deleteTask(String memberId) {
        repository.deleteById(memberId);
        return memberId + "Member Deleted" ;
    }

}


Leave a Comment